You need to sign in to do that
Don't have an account?
Mohit 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.
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.
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.
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
}