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
haroonaharoona 

Bulk Update Using Where clause

Hi,

Can I do something like this in APEX:

 

update leads set company='MyCompany' where fname="myself";

 

This would update mutiple records with the one line command.

 

This of course is standard SQL syntax. Wondering if this is possible on the Force.com platform.

 

Regards

 

-Haroon

Best Answer chosen by Admin (Salesforce Developers) 
h8r41dh8r41d

Hi

 

Unfortunately SOQL only supports SELECT syntax. See this page:

 

http://www.salesforce.com/us/developer/docs/api90/Content/sforce_api_calls_soql.htm

 

What you have to do is query for the rows you want to update, then update the records and save, like so:

 

list<lead> list_to_update = [select company from lead where fname='myself'];

for(lead l : list_to_update) {
     l.company = 'MyCompany';  
}
 
upsert list_to_update;

 

Not quite as convenient as standard SQL, but it works.

 

Let me know if you need any help with anything, it's what we do ;)

 

Gabriel Alack
Contact Us - We Can Help!
Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x122

All Answers

h8r41dh8r41d

Hi

 

Unfortunately SOQL only supports SELECT syntax. See this page:

 

http://www.salesforce.com/us/developer/docs/api90/Content/sforce_api_calls_soql.htm

 

What you have to do is query for the rows you want to update, then update the records and save, like so:

 

list<lead> list_to_update = [select company from lead where fname='myself'];

for(lead l : list_to_update) {
     l.company = 'MyCompany';  
}
 
upsert list_to_update;

 

Not quite as convenient as standard SQL, but it works.

 

Let me know if you need any help with anything, it's what we do ;)

 

Gabriel Alack
Contact Us - We Can Help!
Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x122

This was selected as the best answer
haroonaharoona

Thanks, Gabriel!