Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of utilizing relationships to keep SOQL outside of a loop
List<Account> accList = new List<Account>(
[Select
Id
,Name
,(Select
Email
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
You can’t perform that action at this time.