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
santhiya duraisanthiya durai 

Trigger for updating child when update a field on parent, both parent and child object are same

I have object called Agreement.
One agreement will have mutliple child agreements.
we  have a Parent Agreement lookup field on Child Agreement with Parent agreement value.

When Status category of Child agreement is Terminated, then on Parent agreement Status category and Termination date should be updated as child record.

The issue is am not getting Parent id from my trigger. So its not updating on Parent record eventhough my if condition satisfied.

Class:
public class UpdateTerminationDate {
public static Boolean isFirstTime = true;
   
          }    
 

Trigger:

trigger UpdateTerminationDateTrigger on Apttus__APTS_Agreement__c (after insert,after update) {
     public static Boolean bool = true;
    If(UpdateTerminationDate.isFirstTime){
        UpdateTerminationDate.isFirstTime = false;
   
   
    List<Apttus__APTS_Agreement__c> agr=new List<Apttus__APTS_Agreement__c>();
     List<id> acc=new List<id>();
    for(Apttus__APTS_Agreement__c a:Trigger.New)
    {
        acc.add(a.id);
        system.debug(acc);
    }

    List<Apttus__APTS_Agreement__c> agreement = [select id,Name,Apttus__Parent_Agreement__r.Apttus__Status_Category__c,CLM_Agreement_Termination_Date__c,Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c,Apttus__Parent_Agreement__c,Apttus__Status_Category__c,Apttus__Status__c,Apttus__Parent_Agreement__r.Apttus__Status__c from Apttus__APTS_Agreement__c where id in: acc];    
    
    for(Apttus__APTS_Agreement__c ag:agreement){
        system.debug(ag.Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c);
        if(ag.Apttus__Status_Category__c=='Terminated')
            {
                ag.Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c=ag.CLM_Agreement_Termination_Date__c;
               //ag.Apttus__Status_Category__c=ag.Apttus__Parent_Agreement__r.Apttus__Status_Category__c;
               //ag.Apttus__Status__c='Terminated';
               
                system.debug('test');
              system.debug(ag.Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c);
                system.debug(ag.CLM_Agreement_Termination_Date__c);
                agr.add(ag);
                
            }
        
        update ag;
        }
     
      update agr;
        system.debug(agr);
    
    }
  }

Isuue is both  agr id and ag id are same(child agreement id)
Best Answer chosen by santhiya durai
Agustin BAgustin B
Hi santhiya, the code is quite messy but you should take not the a.id (The child id) but the a.ParentId field (check Apttus__APTS_Agreement__c which is the name of the child reference to the parent).

The  if(ag.Apttus__Status_Category__c=='Terminated') should go on the first iteration, only if that condition is met you add the parent id to the acc list.
You could make a Map that has de Parent Id and the date terminated so in the query you check for the keys and in the iteration you use the id as key to get the terminated date.
Map<Id,Date> acc = new Map<Id,Date>();
 for(Apttus__APTS_Agreement__c a:Trigger.New)    {
    if(a.Apttus__Status_Category__c=='Terminated') {
        acc.put(a.parentId,a.CLM_Agreement_Termination_Date__c);
        system.debug(acc);
    }
}
List<Apttus__APTS_Agreement__c> agreement = [select id,Name,Apttus__Parent_Agreement__r.Apttus__Status_Category__c,CLM_Agreement_Termination_Date__c,Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c,Apttus__Parent_Agreement__c,Apttus__Status_Category__c,Apttus__Status__c,Apttus__Parent_Agreement__r.Apttus__Status__c from Apttus__APTS_Agreement__c where id in: acc.keyset()];    
for(Apttus__APTS_Agreement__c ag:agreement){
ag.CLM_Agreement_Termination_Date__c = acc.get(ag.Id);
}
update ag;

If this solves your issue please mark this answer as correct, it may help others.

All Answers

Agustin BAgustin B
Hi santhiya, the code is quite messy but you should take not the a.id (The child id) but the a.ParentId field (check Apttus__APTS_Agreement__c which is the name of the child reference to the parent).

The  if(ag.Apttus__Status_Category__c=='Terminated') should go on the first iteration, only if that condition is met you add the parent id to the acc list.
You could make a Map that has de Parent Id and the date terminated so in the query you check for the keys and in the iteration you use the id as key to get the terminated date.
Map<Id,Date> acc = new Map<Id,Date>();
 for(Apttus__APTS_Agreement__c a:Trigger.New)    {
    if(a.Apttus__Status_Category__c=='Terminated') {
        acc.put(a.parentId,a.CLM_Agreement_Termination_Date__c);
        system.debug(acc);
    }
}
List<Apttus__APTS_Agreement__c> agreement = [select id,Name,Apttus__Parent_Agreement__r.Apttus__Status_Category__c,CLM_Agreement_Termination_Date__c,Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c,Apttus__Parent_Agreement__c,Apttus__Status_Category__c,Apttus__Status__c,Apttus__Parent_Agreement__r.Apttus__Status__c from Apttus__APTS_Agreement__c where id in: acc.keyset()];    
for(Apttus__APTS_Agreement__c ag:agreement){
ag.CLM_Agreement_Termination_Date__c = acc.get(ag.Id);
}
update ag;

If this solves your issue please mark this answer as correct, it may help others.
This was selected as the best answer
santhiya duraisanthiya durai
@Agustin B
Whether this code will work for vice-versa.
When Parent Agreement is updated with Terminated, then on Child Agreement the termination date should be updated with the value on Parent Record.

Child Agreement having Parent agreement lookup field with value.