function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zahra EL Hassani 10Zahra 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)
    {
    }   
}
Waqar Hussain SFWaqar Hussain SF
Hi Zahra,

It seems you are updating accounts twice. 
Use below code instead. I have commented the update statemenet from inside loop.
 
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)
    {
    }   
}