function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mohit KapoorMohit Kapoor 

Trigger to update values between 2 child records

Hi I have a requirement where in we have 2 objects call one parent and one child. The child object are related to parent and have a lookup relation between the two. The child records are sequence by a field called Order having values 1,2,3,4,5.

So every parent will have 5 children record. 

My requirement is that when the status of child 1 is complete, the Status of child 2 should become pending, i.e. I want updates between 2 child records but I cannot involve parent record. 

If someone can provide a logic it would be great hep.
SivaGSivaG
Hi Mohit,

Please try to use the below trigger. Modify it according to your needs.

trigger child1trigger on Child1_Object__c(after update) {
// Set to store the parent ids
Set<id> parentIdSet = new Set<id>();
//Loop through the child 1 object and get the parent ids of those whose status is complete
  for(Child1_Object__c c : Trigger.New){
    if(c.Status__c == 'Complete' && c.Status__c != Trigger.oldMap.get(c.Id).Status__c)
           parentIdSet.add(c.parent__c);
  }
//Update the Child2 status to Pending  
  if(!parentIdSet.isEmpty()){
    for(Child2_Object__c cc : [Select id,Status__c,parent__c from Child2_Object__c where parent__c IN :parentIdSet){
      cc.Status__c = 'Pending';
      Child2updlist.add(cc);
    }
  }
  if(!Child2updlist.isEmpty()){
   try{
       update Child2updlist;
   }
   Catch(DMLException de){
       System.debug('Exception Occurred on update of child2 object: '+ de);
   }
  }
​}

Thanks
Kumar

PS: Please mark this as Best Answer if this solution solves your problem.
Mohit KapoorMohit Kapoor
Thanks, I was able to make a code without using set and keeping this simple, but now the client has a requirement change. He has brought in a new condition. Lets say in predecessor there are multiple values, say 1,2 then the status sould be set only after both 1 and 2 have status Approved. Attached is the code that i created. Thanks for the previous help. Attached is my code.

trigger Approver_Status_Pending on BNYM_Approvers__c (before update) {

for (BNYM_Approvers__c AQ : Trigger.new)
{
    if(AQ.Status__c != System.Trigger.oldMap.get(AQ.Id).Status__c && AQ.Status__c == 'Approved' ) 
    {
        String AQprojectID = AQ.Id;
        String s1 = String.valueof(AQ.Order__c);     
        String ParentID = AQ.BNYM_Approval_Process__c;
        
        List<BNYM_Approvers__c> parents = [select id, Name, Status__c,Order__c,Predecessor__c from BNYM_Approvers__c where BNYM_Approval_Process__c = :ParentID AND Predecessor__c = : s1];
        
        for(BNYM_Approvers__c s:parents) {
            s.Status__c = 'Pending';
        }
        
        if(parents.size() > 0) {
            update parents;
        }
        
    }//End of Outer IF

}//End of For
}