You need to sign in to do that
Don't have an account?
Zahra EL Hassani 10
Help : A callout failed because an Apex operation has pending uncommitted work
Hello Everyone, In Batch Apex can anyone explain me what this,
"A callout failed because an Apex operation has pending uncommitted work. The Apex code needs to commit or roll back before callouts can be performed in the same transaction"
global class AMOUNT_amount implements Database.Batchable<sObject>{
global Iterable<sObject> start(Database.BatchableContext context)
{
return [SELECT Id, ali_ttc_total_price__c,ali_client_id__c FROM ali_Ticket_Line__b];
}
global static void execute(Database.BatchableContext context, List<ali_Ticket_Line__b> scope){
List<Account> accs = new List<Account>();
Decimal adding_amount=0.0;
for (ali_Ticket_Line__b s : scope)
{
Account acc = [Select id , ali_purchases_amount_ttc_12__c from Account where id=: s.ali_client_id__c];
List <ali_Ticket_Line__b> price=[SELECT id,ali_ttc_total_price__c FROM ali_Ticket_Line__b
WHERE (ali_Ticket_Line__b.ali_client_id__c=: acc.Id)];
system.debug('aaa'+acc.ali_purchases_amount_ttc_12__c);
for(integer i=0;i<price.size();i++)
{
system.debug('bbb'+acc.ali_purchases_amount_ttc_12__c);
adding_amount+=price[i].ali_ttc_total_price__c;
acc.ali_purchases_amount_ttc_12__c=adding_amount;
system.debug('ccc'+acc.ali_purchases_amount_ttc_12__c);
accs.add(acc);
update acc;
}
system.debug('ddd'+acc.ali_purchases_amount_ttc_12__c);
map<id,account> accmap = new map<id,account>();
accmap.putall(accs);
if(accmap.size()>0){
update accmap.values();
system.debug('fff'+accmap.values());
}
system.debug('eee'+acc.ali_purchases_amount_ttc_12__c);
}
}
global void finish(Database.BatchableContext bc)
{
}
}
"A callout failed because an Apex operation has pending uncommitted work. The Apex code needs to commit or roll back before callouts can be performed in the same transaction"
global class AMOUNT_amount implements Database.Batchable<sObject>{
global Iterable<sObject> start(Database.BatchableContext context)
{
return [SELECT Id, ali_ttc_total_price__c,ali_client_id__c FROM ali_Ticket_Line__b];
}
global static void execute(Database.BatchableContext context, List<ali_Ticket_Line__b> scope){
List<Account> accs = new List<Account>();
Decimal adding_amount=0.0;
for (ali_Ticket_Line__b s : scope)
{
Account acc = [Select id , ali_purchases_amount_ttc_12__c from Account where id=: s.ali_client_id__c];
List <ali_Ticket_Line__b> price=[SELECT id,ali_ttc_total_price__c FROM ali_Ticket_Line__b
WHERE (ali_Ticket_Line__b.ali_client_id__c=: acc.Id)];
system.debug('aaa'+acc.ali_purchases_amount_ttc_12__c);
for(integer i=0;i<price.size();i++)
{
system.debug('bbb'+acc.ali_purchases_amount_ttc_12__c);
adding_amount+=price[i].ali_ttc_total_price__c;
acc.ali_purchases_amount_ttc_12__c=adding_amount;
system.debug('ccc'+acc.ali_purchases_amount_ttc_12__c);
accs.add(acc);
update acc;
}
system.debug('ddd'+acc.ali_purchases_amount_ttc_12__c);
map<id,account> accmap = new map<id,account>();
accmap.putall(accs);
if(accmap.size()>0){
update accmap.values();
system.debug('fff'+accmap.values());
}
system.debug('eee'+acc.ali_purchases_amount_ttc_12__c);
}
}
global void finish(Database.BatchableContext bc)
{
}
}
Refer below help article for solution.
https://help.salesforce.com/articleView?id=000079772&type=1
https://help.salesforce.com/articleView?id=000003701&type=1
https://developer.salesforce.com/forums/?id=906F00000008vMmIAI
Best Regards,
Sandhya
It seems you are updating accounts twice.
Use below code instead. I have commented the update statemenet from inside loop.