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
j_panchalj_panchal 

how to perform multiple objects update in salesforce.

Hello,

 

i'm getting the error - 'You have uncommitted work pending. Please commit or rollback before calling out'  when performing  updates for multiple object or calling multiple update statements.

 

i have two sObjects 1) Contact and 2) Account as below -

 

sContact = [SELECT c.FirstName, c.LastName, c.Title FROM Contact c WHERE c.Id = :contactId];
sAccount = [SELECT a.Id, a.Name,a.Website FROM Account a WHERE a.Id = :accountId];

 

after this inside a loop i'm updating some filed values of both objects as -

 

   for(Integer i=0 ; i<selRecords.size() ; i++)
     {
              if(selRecords[i].AttributeType == 'contact')
                    {
                        sContact.put(selRecords[i].FieldName,selRecords[i].fieldValue);
                    }
                    if(selRecords[i].AttributeType == 'CompanyType')
                    {
                        sAccount.put(selRecords[i].FieldName,selRecords[i].fieldValue);
                    }
         }
     

               update sContact;
               update sAccount;

 

during the call "update sAccount" it throws the error - You have uncommitted work pending. Please commit or rollback before calling out.

 

i tried Database.SaveResult[] SR = database.update(updatesArr);   also, where updatesArr is array of SObject in which both of the above sObjects are assigned, but can't get it working.

 

Please suggest that how can we call multiple update statements or perform multiple sObject updates.

 

Thanks in advance.

          

Kunal01Kunal01

Hi ,

 

Not sure what is the exact issue but while going through the code, I see that You are performing update ove a MAP (sContact, sAccount). Try to make it as LIST and than update it.

 

Thanks,

kunal

j_panchalj_panchal

Hi,

 

Thanks for the response. i tried using list also, if we use MAP or List the update works fine in both cases but after performing update it throws an error saying that 'You have uncommitted work pending. Please commit or rollback before calling out.'

 

i need to display an alert box after the update with message 'Record updated', or if any error comes up then the error message should be displayed on alert box, however if we perform any operation (like display the alert box)  after update statements then it throws the above specified error.

 

Thanks again.

 

PhoenixedPhoenixed

Try to update Account first and than trry to update contact.

 

Thanks