Skip to content

Instantly share code, notes, and snippets.

@patmcclellan
Last active October 2, 2020 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patmcclellan/86c2cb3fcc267887eac8ad3899be12b7 to your computer and use it in GitHub Desktop.
Save patmcclellan/86c2cb3fcc267887eac8ad3899be12b7 to your computer and use it in GitHub Desktop.
Lists vs. Maps example
List<Account> accountList = [SELECT Id, Name, BillingState FROM Account LIMIT 3];
System.debug('accountList: ' + accountList);
/* output from the log— just what you picture for a list of SObjects
USER_DEBUG [2]|DEBUG|accountList: (
Account:{Id=0013t00001YbRqrAAF, Name=Edge Communications-top, BillingState=TX},
Account:{Id=0013t00001YbRqsAAF, Name=Burlington Textiles Corp of America-top, BillingState=NC},
Account:{Id=0013t00001YbRqtAAF, Name=Pyramid Construction Inc.-top}
)
*/
Map<Id,Account> accountMap = new Map<Id,Account>([SELECT Id, Name, BillingState FROM Account LIMIT 3]);
System.debug('accountMap: ' + accountMap);
/* output from the log— notice here end of line 18 and line 22, the map is enclosed in {},
which tells you this is an 'object' (a map in this case).
USER_DEBUG [5]|DEBUG|accountMap: {
0013t00001YbRqrAAF=Account:{Id=0013t00001YbRqrAAF, Name=Edge Communications-top, BillingState=TX},
0013t00001YbRqsAAF=Account:{Id=0013t00001YbRqsAAF, Name=Burlington Textiles Corp of America-top, BillingState=NC},
0013t00001YbRqtAAF=Account:{Id=0013t00001YbRqtAAF, Name=Pyramid Construction Inc.-top}
}
You have the same 3 accounts as you had in accountList,
but, they are INDEXED by their respective IDs.
So, if I wanted to access the Burlington Textiles account,
I could simply say:
Account burlAcc = accountMap.get('0013t00001YbRqsAAF');
*/
@asmitakhare
Copy link

Like this code. It's absolutely good motivation for us to try different things.

@radwomenunazhang
Copy link

Thank you Pat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment