Last active
November 26, 2021 02:30
-
-
Save dancinllama/70919d333c3db177485c to your computer and use it in GitHub Desktop.
Example of utilizing relationships to keep SOQL outside of a loop
This file contains 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> accList = new List<Account>( | |
[Select | |
Id | |
,Name | |
,(Select | |
From | |
Contacts | |
) | |
From Account] | |
); | |
List<Account> accountsToUpdate = new List<Account>(); | |
for(Account acc : accList){ | |
Integer numWithEmail = 0; | |
for(Contact cont : acc.Contacts){ | |
if(!String.isEmpty(cont.Email)){ | |
numWithEmail++; | |
} | |
} | |
acc.Contacts_With_Email_Address__c = numWithEmail; | |
accountsToUpdate.add(acc); | |
} | |
update accountsToUpdate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment