+ Start a Discussion
TarentTarent 

update multiple objects records from single way

Hi everyone

Can we update any of the field except ‘id ’more than 2 records simultaneously from single list. Is it possible?

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

list<sobject> sobjecttest=new list<sobject>();

 

list<account> acc=[select id ,name from account limit 1000];

 

if(acc.size()>0)

{

                acc[0].name=acc[0].name+'test';

                sobjecttest.add(acc[0]);

}

list<contact>con=[select firstname from contact limit 1000];

if(con.size()>0)

{

                con[0].firstname=con[0].firstname+'test';

                sobjecttest.add(con[0]);

}

update sobjecttest;

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

list<account> acc=[select id ,name from account limit 1000];

for(integer i=0;i< acc.size();i++)

{

                acc[i].name=acc[i].name+' test '+string.valueof(i);

               

}

update acc;

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

TarentTarent

Hi ankit

 

Thanks but i want to update more than 2 records which has different types of object  and update from single list. 

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

list<sobject> sobjecttest=new list<sobject>();

 

list<account> acc=[select id ,name from account limit 1000];

 

if(acc.size()>0)

{

                acc[0].name=acc[0].name+'test';

                sobjecttest.add(acc[0]);

}

list<contact>con=[select firstname from contact limit 1000];

if(con.size()>0)

{

                con[0].firstname=con[0].firstname+'test';

                sobjecttest.add(con[0]);

}

update sobjecttest;

This was selected as the best answer