Salesforce Collections in Use

The last few blog posts, we touched the surface of the different collections available in Salesforce and what they can hold. Now let’s take a look at them in action inside of a trigger on the Account object. The following trigger looks for changes in the Account address and when there is a change, updates the child Contact records that are flagged to use the Account’s address. To follow along in your dev org with this example, you will need to add a checkbox field to the Contact object with the API name of “Use_Account_Address__c”.

We also need to keep in mind that with every Trigger, there are Trigger Context Variables that are available for us to use. In this example we are going to use three of them: – a list of updated records

Trigger.newMap – a map of updated records where the Key is the Id and the Value is the updated record

Trigger.oldMap – a map of the records before the update occurs where the Key is the Id and the Value is the original record

Let’s break down what we are doing at each line of code:

Line 6: The trigger definition which states we only want this trigger to run after an Account has been updated.

Line 8: Create a Set variable to hold all of the Ids of the Accounts being updated.

Line 10: Create a List of Contacts to hold all of the Contact records that will need their Addresses updated.

Line 12: Loop through each Account inside the list.

Line 14: Create a variable to hold the Account record before it was updated, we use the Account’s Id to access the Trigger.oldMap which returns to us the original Account record before the modifications were made.

Lines 16 – 19: Compare the addresses in the updated Account record to the original Account record and if any of the address fields do not match continue onto line 20.

Line 20: Add the Account’s Id to the Set we created above because there was a change.

Line 23: Loop through each Contact that are children of the Accounts where the address changed and where the Contact is set to use the Account’s address.

Line 32: Create a variable to hold the parent Account record with its updates using the Trigger.newMap.

Lines 34 – 37: Update the Contacts Mailing Address with its parent’s Address fields.

Line 39: Add the updated Contact to a list so we can update all of the Contacts at the same time.

Line 42: Update the Contacts in the list.

Now let’s take a look at how we could accomplish the above task manually. Let’s say we had an intern working for us who is going to verify all of the Account addresses and phone numbers in our Salesforce Org, but they do not have a Salesforce login. More than likely we are going to give the intern an export of our Account records from Salesforce so they can do their investigation and updates.

The intern went through row by row and googled each Account. Some Accounts had no changes, others had address changes while others had just phone number changes and then there were some that had both address and phone number changes. Unfortunately, the intern didn’t highlight which records had address changes and although we could probably update all of the Contacts we really only want to update the Contacts where their parent Account address had a change.

Luckily we saved the original excel file and can compare that to the intern’s updated spreadsheet by doing a v-lookup on the Account Id on the original spreadsheet to match to the updated spreadsheet to see if a change was made. Once we have identified that an account has changed we grab its Account Ids and keep a comma separated list.

Now we can use the report builder to find all the Contacts that were set to Use the Account Address by using the Filter to only return Contacts when the Use the Account Address field is set to True and that have an Account Id that is equal to any in our comma separated list.


Now with the Contacts export, we can once again use a v-lookup to the updated Account spreadsheet to grab the address and update the address on the matching Contact Record. Once the Contact spreadsheet is all updated we can use the data loader to update the Contacts in Salesforce.

Another way to accomplish this task is by using the process builder where no code or spreadsheets are required. If you want a walkthrough on how to do that, take a look at the Quick Start: Lightning Process Builder project on Trailhead.

Leave a comment

Your email address will not be published. Required fields are marked *