This course is currently a work in progress. Please join the waitlist to receive regular updates.

Update collections in apex

When updating multiple records, it’s recommended to use collections. This makes your code more efficient by reducing database resources and staying within salesforce’s governor limits.

To update the records in a collection:

  1. 1. Query the records that you want to update into a collection. e.g list.
  2. 2. Modify the field values of each record using afor loop for a collection.
  3. 3. Use a single DML statement to update the entire collection.

Following is an example of updating account industry to 'Technology' from a SOQL query results :

Loading...

In the above example:

  • A list collection accountUpdateList is declared to hold a list of accounts.
  • A SOQL query is used query on accounts to query the existing Account records from salesforce database.
  • The industry field for all records are updated to 'Technology' using a for loop.
  • Finally, the DML Update statement is used to update the collection of account records into salesforce.

Try It Yourself

Loading...