在订单产品对象上,我与第三方系统集成,每当通过 Queueable 类订购产品时,该系统都会向第三方发送数据。
现在,当使用以下类的批处理顶点将订单产品中的特定字段更新为特定值时,我尝试将一组记录插入到另一个对象。
global class CreateScheduleRecords implements Database.Batchable<sObject> {
//List<OrderItem> orderProductList = new List<OrderItem>();
Map<Id, OrderItem> IdOrderItemMapBatch = new Map<Id, OrderItem>();
List<Schedule__c> scheduleRecordList = new List<Schedule__c>();
global CreateScheduleRecords(Map<Id, OrderItem> IdOrderItemMap) {
IdOrderItemMapBatch = IdOrderItemMap;
System.debug('Akshay is checking the schedule batch ' + IdOrderItemMapBatch);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([SELECT Id, Order.Id, UnitPrice, Quantity, Pacing__c, OL_Left_To_Deliver__c, Line_Item_Start_Date__c, Line_Item_End_Date__c FROM OrderItem WHERE Id in :IdOrderItemMapBatch.keySet()]);
}
global void execute(Database.BatchableContext BC, List<OrderItem> orderProductList) {
Decimal deliveryQuantity;
Decimal toBeUpdatedQuantity;
Integer DeliveryQuantityRemainder;
String ScheduleDetailsFuture;
System.debug('Akshay 2 is checking the Queried records ' + orderProductList);
try {
for(OrderItem oli : orderProductList) {
Date a = oli.Line_Item_Start_Date__c;
Date b = oli.Line_Item_End_Date__c;
Integer monthDiff = a.monthsBetween(b);
if (b.day() > a.day()) monthDiff++;
System.debug('Akshay 3 is checking the differnce in the months ' + monthDiff);
if(oli.Pacing__c == 'Even') {
deliveryQuantity = oli.OL_Left_To_Deliver__c / monthDiff;
DeliveryQuantityRemainder = math.mod(Integer.valueOf(oli.OL_Left_To_Deliver__c), monthDiff);
} else if(oli.Pacing__c == 'Front Load') {
deliveryQuantity = oli.OL_Left_To_Deliver__c;
}
for(Integer i=0; i<monthDiff; i++) {
if(oli.Pacing__c == 'Even') {
if(DeliveryQuantityRemainder == 0) {
toBeUpdatedQuantity = deliveryQuantity;
} else {
if(i != 1) {
toBeUpdatedQuantity = Math.floor(deliveryQuantity);
} else {
toBeUpdatedQuantity = Math.floor(deliveryQuantity) + DeliveryQuantityRemainder;
}
}
} else if(oli.Pacing__c == 'Front Load') {
if(i == 0) {
toBeUpdatedQuantity = deliveryQuantity;
} else {
toBeUpdatedQuantity = 0;
}
}
Schedule__c sc = new Schedule__c();
sc.Delivery_Date__c = System.Date.today().toStartOfMonth();
sc.Schedule_Status__c = 'Live';
sc.Schedule_Type__c = 'Monthly Forecast';
sc.Order_Product__c = oli.Id;
sc.Order__c = oli.Order.Id;
sc.Unit_Price__c = oli.UnitPrice;
sc.Quantity__c = toBeUpdatedQuantity;
scheduleRecordList.add(sc);
}
System.debug('Akshay 4 is checking the Schedule record list ' + scheduleRecordList);
if(scheduleRecordList != null && !scheduleRecordList.isempty()) {
System.enqueueJob(new MyQueueable(scheduleRecordList));
}
}
} catch(Exception e) {
System.debug('an error occured while updating the lineitem records ' + e.getMessage() + ' line number ' + e.getLineNumber());
}
}
global void finish(Database.BatchableContext BC) {
}
public class MyQueueable implements Queueable {
List<Schedule__c> records;
public MyQueueable(List<Schedule__c> records) {
this.records = records;
}
public void execute(QueueableContext context) {
insert records;
}
}
}
现在,当我尝试更新此字段时,两个单独的可排队作业同时运行,这反过来又产生错误“添加到队列中的可排队作业太多:2” 一个是正在更新第三方的标注,第二个是我正在运行的标注,用于使用订单项字段更新在另一个对象中创建一批记录。
我尝试使用 @future 方法希望它可以解决这个问题,但它不起作用,因为它也是一个可排队的方法。
有人可以告诉我如何解决这个问题吗?如果您需要更多详细信息,请告诉我。预先感谢。
您确定要为每个订单项目都有一个 ScheduleRecords 排队作业吗? 也许糟糕的是同时创建了太多排队的工作。 而且 - 跨排队作业 - 对相同的 OrderItem 进行 DML。
考虑:
添加到 SELECT 语句:
按 Order.Id、Id 排序
然后将每个订单的列表放入作业中。