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
abhi_sfdcabhi_sfdc 

System.CalloutException: You have uncommitted work pending.

Hi All,

 

I am getting this error ("System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out") on one custom button(Delete) click in salesforce.

In below code  i am geting the records from two instances and trying to delete the selected record: 

 

public with sharing class testchkcnt{

    public List<Accountwrapper> accntlist{get; set;}
    public List<Case> caselist{get; set;}
    public List<Account> selectedaccount = new List<Account>();
    public List<Cse> cselist{get; set;}
    public String lname{get; set;}
    Boolean testval = false;
    Boolean testcase = false;
    public String testcaseid{get; set;}

    public Class Cse
    {
        public String CaseNumber{get; set;}
        public String Status{get; set;}
    }   
    
    public pageReference search(){
        List<Accountwrapper> accnt1 = new List<Accountwrapper>();
        List<Accountwrapper> accnt2 = new List<Accountwrapper>();
        accntlist = new List<Accountwrapper>();
        List<Account> accn = [Select Id, Name, Phone from Account where Name =: lname] ;
        for(Account a: accn ){
            accnt1.add(new Accountwrapper(a));
        }
        partnerSoapSforceCom.Soap myPartnerSoap = new partnerSoapSforceCom.Soap();
        partnerSoapSforceCom.LoginResult partnerLoginResult = myPartnerSoap.login('test123@xxx.com', '**********');
        soapSforceComSchemasClassAllacoutsdd.SessionHeader_element webserviceSessionHeader = new soapSforceComSchemasClassAllacoutsdd.SessionHeader_element();
        webserviceSessionHeader.sessionId = partnerLoginResult.sessionId;
          
            soapSforceComSchemasClassAllacoutsdd.allAcoutsDetails  stub = new soapSforceComSchemasClassAllacoutsdd.allAcoutsDetails();
            stub.SessionHeader = webserviceSessionHeader;
            List<Account> lst_Account = new List<Account>();
            if(stub.makeaccount(lname) != null){
            for(soapSforceComSchemasClassAllacoutsdd.response li: stub.makeaccount(lname)){
                
                    Account acn = new Account();
                    acn.Id = li.AccId;
                    acn.Name = li.AccName;
                    acn.Phone = li.PhoneNum;
                    lst_Account.add(acn);
                }
            }

            for(Account an: lst_Account){
                accnt2.add(new Accountwrapper(an));
            }
        accntlist.addall(accnt1);
        accntlist.addall(accnt2);       
        if(accntlist.size()>0)
            setHideshow(true);
        else
            setHideshow(false);
        
        return null;
    } 
        
    public Boolean gethideshowcase()
    {
        return this.testcase ;
    }
    public void sethideshowcase(boolean q)
    {
        this.testcase = q;
    }
    
       public Boolean getHideshow()
    {
        return this.testval;
    }
    public void setHideshow(boolean s)
    {
        this.testval = s;
    }

    public pageReference GetCaseDetails(){
        caselist = new List<Case>();
        string st = ApexPages.currentPage().getParameters().get('testcaseid');
        caselist  = [Select Id, CaseNumber, Status from Case where AccountId =: st];
        cselist = new List<Cse>();
        sethideshowcase(true);

        if(caselist.size() == 0){
            partnerSoapSforceCom.Soap myPartnerSoap = new partnerSoapSforceCom.Soap();
            partnerSoapSforceCom.LoginResult partnerLoginResult = myPartnerSoap.login('test123@xxx.com', '*********');
            soapSforceComSchemasClassAllacoutsdd.SessionHeader_element webserviceSessionHeader = new soapSforceComSchemasClassAllacoutsdd.SessionHeader_element();
            webserviceSessionHeader.sessionId = partnerLoginResult.sessionId;
            
            soapSforceComSchemasClassAllacoutsdd.allAcoutsDetails  stub = new soapSforceComSchemasClassAllacoutsdd.allAcoutsDetails();
            stub.SessionHeader = webserviceSessionHeader;          
            for(soapSforceComSchemasClassAllacoutsdd.response lis: stub.allcasedetails(st)){
                Cse cs = new Cse();
                cs.CaseNumber = lis.cnumb;
                cs.Status = lis.Casestate;
                cselist.add(cs);
            }  
        }
        else
            {
            System.debug('Entering...');
                for(Case c: caselist)
                {
                    Cse cs = new Cse();
                    cs.CaseNumber = c.CaseNumber;
                    cs.Status = c.Status;
                    cselist.add(cs);
                }
            }
        if(cselist.size()==0)
            sethideshowcase(false);
        return null;
        }
   
    public pageReference deleteaccount(){
        List<Account> selAccounts=GetSelectedAccounts();
        List<Account> act_del = new List<Account>();
        if(selAccounts.size()>0){
            for(Integer j=0; j<selAccounts.size(); j++){
                act_del.add(selAccounts[j]);
            }
           delete act_del; 
            
        }
        return search();           
    }
    
    public List<Account> GetSelectedAccounts(){
        if(selectedaccount.size()>0){
            system.debug('##########' + selectedaccount);
            return selectedaccount ;  
        }     
        else{
            return null;
        }
    }
    
   public pageReference GetSelected(){
        selectedaccount.clear();
        system.debug('testingggggggggg');
        system.debug('abhi@@@@@@@' + accntlist);
        for(Accountwrapper accwrapper: accntlist)           
            if(accwrapper.selected == true)
                selectedaccount.add(accwrapper.acc);
        return null;
    }
     
    public class Accountwrapper{
        public Account acc{get; set;}
        public Boolean selected{get; set;}
        public Accountwrapper(Account a){
            acc = a;
            selected = false;
        }
    }    
}

 

Can someone help me how to resolve this issue?

 

Avidev9Avidev9
Just try to change the follow of your class.
Before doing any DML, make callouts to the webservice. This will resolve ur problem