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
Matt Cooper 7Matt Cooper 7 

Updating Field Value On Parent Record When Child Record Value Changes - Lookup Relationship

Hi,

I have a situation where I have a record type that wants to include an amend type of functionality.  Basically, a record will be created and completed.  Sometimes, a user will have to amend the information to the record, but the user wants to leave the old record alone and just reference back to the old record with a Lookup field.  The functionality I am trying to create is a trigger that would update a field on the parent record to 'Amended' when the child record becomes 'Activated.'

I have attempted to create a trigger to accomplish this, but when I test it, nothing happens.  Can someone help me figure out where I've gone wrong?  

Object: Apttus__APTS_Agreement__c
Field to update on parent record: Apttus__Status_Category__c
Field on child record to look at: Apttus__Status__c

So when Apttus__Status__c = 'Activated' on the child record, I want Apttus__Status_Category__c = 'Amended' on the parent record.
 
trigger AR_Variance_Status_Update on Apttus__APTS_Agreement__c (before insert, before update) {
 
    Map<ID, Apttus__APTS_Agreement__c> parentAgmts = new Map<ID, Apttus__APTS_Agreement__c>();
    List<Id> listIds = new List<Id>();
    
    for (Apttus__APTS_Agreement__c childObj : Trigger.new) {
    	listIds.add(childObj.Variance_For__c);
    }
    
    parentAgmts = new Map<Id, Apttus__APTS_Agreement__c>([SELECT id, Name FROM Apttus__APTS_Agreement__c WHERE ID IN :listIds]);   
    
    for (Apttus__APTS_Agreement__c agmt :trigger.new)
    {
        Apttus__APTS_Agreement__c myParentAgmt = parentAgmts.get(agmt.Variance_For__c);
        
        if(agmt.Apttus__Status__c == 'Activated' && agmt.Variance_For__c <> null)  {
            myParentAgmt.Apttus__Status_Category__c = 'Amended';
        }
    }
}

Thanks!
Matt
Best Answer chosen by Matt Cooper 7
Balaji BondarBalaji Bondar
Hi Matt,

You have to update the parent records :
I have updated the trigger:
trigger AR_Variance_Status_Update on Apttus__APTS_Agreement__c (before insert, before update) {
 
    Map<ID, Apttus__APTS_Agreement__c> parentAgmts = new Map<ID, Apttus__APTS_Agreement__c>();
    List<Id> listIds = new List<Id>();
	List<Apttus__APTS_Agreement__c> ApttusAPTSAgreementList = new List<Apttus__APTS_Agreement__c>();
    
    for (Apttus__APTS_Agreement__c childObj : Trigger.new) {
    	listIds.add(childObj.Variance_For__c);
    }
    
    parentAgmts = new Map<Id, Apttus__APTS_Agreement__c>([SELECT id, Name FROM Apttus__APTS_Agreement__c WHERE ID IN :listIds]);   
    
    for (Apttus__APTS_Agreement__c agmt :trigger.new)
    {
        Apttus__APTS_Agreement__c myParentAgmt = parentAgmts.get(agmt.Variance_For__c);
        
        if(agmt.Apttus__Status__c == 'Activated' && agmt.Variance_For__c <> null)  {
            myParentAgmt.Apttus__Status_Category__c = 'Amended';
			ApttusAPTSAgreementList.add(myParentAgmt);
        }
    }
	update ApttusAPTSAgreementList;
}
Note:Make sure that your trigger is not getting called recursively.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

All Answers

Balaji BondarBalaji Bondar
Hi Matt,

You have to update the parent records :
I have updated the trigger:
trigger AR_Variance_Status_Update on Apttus__APTS_Agreement__c (before insert, before update) {
 
    Map<ID, Apttus__APTS_Agreement__c> parentAgmts = new Map<ID, Apttus__APTS_Agreement__c>();
    List<Id> listIds = new List<Id>();
	List<Apttus__APTS_Agreement__c> ApttusAPTSAgreementList = new List<Apttus__APTS_Agreement__c>();
    
    for (Apttus__APTS_Agreement__c childObj : Trigger.new) {
    	listIds.add(childObj.Variance_For__c);
    }
    
    parentAgmts = new Map<Id, Apttus__APTS_Agreement__c>([SELECT id, Name FROM Apttus__APTS_Agreement__c WHERE ID IN :listIds]);   
    
    for (Apttus__APTS_Agreement__c agmt :trigger.new)
    {
        Apttus__APTS_Agreement__c myParentAgmt = parentAgmts.get(agmt.Variance_For__c);
        
        if(agmt.Apttus__Status__c == 'Activated' && agmt.Variance_For__c <> null)  {
            myParentAgmt.Apttus__Status_Category__c = 'Amended';
			ApttusAPTSAgreementList.add(myParentAgmt);
        }
    }
	update ApttusAPTSAgreementList;
}
Note:Make sure that your trigger is not getting called recursively.

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
This was selected as the best answer
Matt Cooper 7Matt Cooper 7
Of course! Good ole brainfart there. Thanks Balaji!