0

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)

1 Answer 1

0

The reason they are both the same, is you are not adding the parent records to the list to be updated, but the childs. You do need to re-query the child records to get the parent data, but when you do this

ag.Apttus__Parent_Agreement__r.CLM_Agreement_Termination_Date__c=ag.CLM_Agreement_Termination_Date__c;

that isn't going to update the parent record. Try this

trigger UpdateTerminationDateTrigger on Apttus__APTS_Agreement__c (after insert,after update) {

    if(UpdateTerminationDate.isFirstTime){
        UpdateTerminationDate.isFirstTime = false;

        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 :Trigger.New 
                                                     AND Apttus__Status_Category__c = 'Terminated'
                                                     AND Apttus__Parent_Agreement__c != null];  

        Map<Id, Apttus__APTS_Agreement__c> parentAgreements = new Map<Id, Apttus__APTS_Agreement__c>();

        for(Apttus__APTS_Agreement__c ag : agreement){
            if(!parentAgreements.containsKey(ag.Apttus__Parent_Agreement__c)) {

                Apttus__APTS_Agreement__c p = new Apttus__APTS_Agreement__c();
                p.Id = ag.Apttus__Parent_Agreement__c;
                p.CLM_Agreement_Termination_Date__c = ag.CLM_Agreement_Termination_Date__c;

                parentAgreements.put(ag.Apttus__Parent_Agreement__c, p);
            }

        }

        update parentAgreements.values();    
    }
}
2
  • Thanks for your Update. Can we update in vice-versa. When field on Parent is updated then on child record Termination date should be same as parent. Commented Apr 23, 2020 at 10:19
  • how to test this class . I have tried , but coverage has stopped @22% Commented May 8, 2020 at 6:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .