提问人:Akshay Vasu 提问时间:11/17/2023 最后编辑:marc_sAkshay Vasu 更新时间:11/21/2023 访问量:24
我有两个在事务中运行的可排队作业如何解决它
I have two queueable jobs running in a transaction how to resolve it
问:
在 Order Product 对象上,我与第三方系统集成,每当 Order Product 通过 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 difference 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;
}
}
}
现在,当我尝试更新此字段时,两个单独的可排队作业同时运行,这反过来又会产生错误“Too many Queueable jobs added to the queue: 2” 一个是用于更新第三方的标注,第二个是我正在运行的标注,以使用 order Item 字段 update 在另一个对象中创建一批记录。
我尝试使用@future方法,希望它能解决这个问题,但它不起作用,因为它也是一个可排队的方法。
有人可以告诉我如何解决这个问题吗?如果您需要更多详细信息,请告诉我。提前致谢。
答:
0赞
Emmett
11/18/2023
#1
是否确定要为每个 orderItem 设置一个 ScheduleRecords 的排队作业? 也许不好的是同时创建了太多排队的作业。
考虑:
添加到 SELECT 语句中:
按 Order.Id 订购,ID
然后,将包含每个订单列表的作业排入队列。
评论