How To Program In Apex While Dealing With Two Lists

Published on
January 21, 2015

Visualize a tightrope walker performing at a height of 520 feet from the ground trying to balance on the rope putting on a brave face all the time. Writing a program which is meant to be executed on the cloud is as tricky as the rope walker’s precarious struggle for balance. Of course, it comes minus the fear of falling from great heights with broken bones.

Traditional programming has had its glorious run but writing program for cloud platforms is the flavour of the season. Cloud platforms, with all its strappings and rules, impose several limitations on the developer. It requires the developer to put in double the efforts to make the code more efficient while ensuring less utilization of cloud resources.

For all the Salesforce developers working on Apex, AppShark is launching a series of posts on Apex programming which will provide solutions to some common coding problems drawn from over 7 years of experience on several projects.

In the first of the series on ‘’Apex Programming Tips and Tricks’’, we will show youhow to program in Apex while dealing with two lists and the purpose is to search for a data element in second list correlating to the first list.

The Solution lies in designing the program in a way to search for the Parent Account field of all the accounts where the Parent Account is null. The search should involve checking the ‘Parents’ using Parent External ID field (legacy data), which is present on all the child accounts.


1) First, retrieve all the Accounts to update:

List<Account> AccountsToUpdate = [SELECT Id,parentExternalID__c,parentId FROM Account where parentId == null];

2) Now, we need to retrieve all the Accounts which are Parents:

List<Acocunt> ParentAccounts = [SELECT Id,ExternalID__c FROM Account where parentExternalID__c = null]

3) The easiest method with which we can do this is by writing two different loops:

for(Account objAcc : AccountsToUpdate){

for(Account objParentAccount : ParentAccounts){

//Compare the external ID field and update the child accounts



4) Above said code can reach various governing limits like CPU time limit etc as it takes more time to execute. Therefore, the best way is to use maps:

Create a map of Parent Accounts

map<string,Account> mapParents = new map<string,Account>();

for(Account objParentAccount : ParentAccounts){

mapParents.put(objParentAccount. ExternalID__c,objParentAccount);


There is another way to directly create the map from SELECT Statement, which we will discuss in the next series…

5) As you have the map ready, all you need to do is to run a loop of child accounts and then find the correct parent in the map:

for(Account objAcc : AccountsToUpdate){

if(mapParents.get(objAcc. parentExternalID__c) != null)

objAcc. parentId = mapParents.get(objAcc.parentExternalID__c);


Voila! This code should do the trick in an easy way as we are eliminating the inner loop. Hence, there will be only 2 statements executed for each Account record. Here’s hoping this code will make your job easier especially if you are struggling right now to put together the right code.

Happy Programming!

Watch out for part 2 in this series next week…

Setup a consultation