Last active
August 29, 2015 14:19
-
-
Save victorabraham/326472a374dcf8d0c5e3 to your computer and use it in GitHub Desktop.
This is the bulkified version of Non-Bulkified code
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
//Declaring initial list to hold list of child records for single update at last | |
List<child__c> childUpdateList = new List<child__c>(); | |
//Child query is used to return both child and parent records in single query | |
for(Parent__c parent: [SELECT Id,name,Status__c,(SELECT Id,Name,Parent__c, child_Status__c FROM Child__r) | |
FROM Parent__c]) | |
{ | |
if(parent.Status__c =='Completed') | |
{ | |
//If parent status is completed child record status is changed and added into a list | |
for(child__c child:parent.Child__r) | |
{ | |
child.child_status__c ='Completed'; | |
childUpdateList.add(child); | |
} | |
} | |
} | |
//At the end if there are child records to be updated, it is updated | |
if(!childUpdateList.isEmpty()) | |
{ | |
update childUpdateList; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment