Last active
October 2, 2020 05:17
-
-
Save patmcclellan/86c2cb3fcc267887eac8ad3899be12b7 to your computer and use it in GitHub Desktop.
Lists vs. Maps example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Like this code. It's absolutely good motivation for us to try different things.