As a Salesforce Admin we often need to update a bunch of records. We often find ourselves downloading the data, manipulating in excel and then doing an update immediately after, which is quite a tedious process.
The fastest way to do this – is to use APEX.
Many admins are scared to even look at code, so I’ve simplified this and hopefully if you follow my steps you’ll be able to adapt this to do whatever you need it to!
I’ve used a simple edit on the status of a case for my example – where we want to take all open phone cases (Origin = Phone) and Close them.
Step 1: (Open the window where the code will run)
Open the developer console and click the menu option Debug – then click Open Execute Anonymous Window (Ctrl+e)
Step 2 (Build Your Query and Chose What You Want to Update)
I’m using the standard Salesforce object for Cases – called case. The update is broken down into 2 parts – firsts we query the records we want to work with and save them to an array – I’ve called this array exactly what it is ‘recordsToUpdate’ – once we save the records we will be working with – we then loop through them all one by one – I’ve called this ‘singleRecord’ . The loop function is the for – and we are going individually across every record that is saved in our array named ‘recordsToUpdate’. Inside of this loop is where we manipulate our data – I’ve taken the status and changed it closed:

List<Case> recordsToUpdate = [select Id, status from Case where origin = 'Phone' and isclosed = false limit 100];
for(Case singleRecord: recordsToUpdate) {
singleRecord.status = 'Closed';
}
update recordsToUpdate;
Now, lets say you wanted to update this with your own fields and queries. Quite Simple!
- Replace ‘Case’ with the object you are working with
- The query is the select statement – so that also needs to be changed – however remember you need the ID of any record you want to update so leave that there – and if you want to update a record you also need it in the select part of your query.
- Now the update part singleRecord.status is actually case.status. Let’s adapt that to whatever you will be using – so replace the .status part with whichever field you want to update.
- Lastly you can add multiple fields to update, just make a new line after the ‘;’ and type SingleRecord.{FieldYouWantToUpdate} = ‘New Info’;
Always remember when updating records via APEX execute to keep the size small using that Limit at the end of your query.
And as with anything – feel free to reach out to me by clicking Contact Me if you have any questions on how to apply this to your Salesforce used case!