0

I am trying to solve a simple problem. I have an object Service_Expansion_Tracker__C, there is a lookup field NHC_Tracker__c on the object (The relation between NHC tracker and Service Expansion Tracker is one - one).

I have a field called Project_Status__c on Service Expansion Tracker. I want to update the Service Expansion Project Status field on the NHC tracker when the Project status on Service Expansion Tracker is updated. (the picklist values are the same)

Also, I want to send an email as well, saying that status has now been updated

The trigger below works when I try to update NHC Tracker with a hardcoded value. I am unable to bring the actual value of project status from the service expansion tracker record which was updated.

trigger ILM_ExpansionProjectStatusUpdate on Service_Expansion_Tracker__c (before insert, before update, after update,after insert,after delete, before delete) {
    if(Trigger.isAfter){
              
        if(Trigger.isUpdate){
        updateRelatedNHCRecords(Trigger.new,Trigger.oldMap);
        }                
    
    }
    
    
       private void updateRelatedNHCRecords(List<Service_Expansion_Tracker__c> expansionTrackerList, Map<Id,Service_Expansion_Tracker__c> expansionTrackerMapOld){
        
        Set<String> fieldnames = new Set<String>{'Project_Status__c'};
            
         List<Id> trackerIds = new List<Id>();    

            
        for(Service_Expansion_Tracker__c trackerObj : expansionTrackerList){
            for(String fieldName : fieldnames){
                if(trackerObj.get(fieldName) != expansionTrackerMapOld.get(trackerObj.Id).get(fieldName)){
                    system.debug(':::::'+fieldName);
                    trackerIds.add(trackerObj.NHC_Tracker__c);
                }
            }
        }
           
        system.debug(':::::'+trackerIds);
        
        List<NHC_Tracker__c> listToUpdate = [Select Id, Service_Expansion_Project_Status__c from NHC_Tracker__c where Id IN:trackerIds];
        for(NHC_Tracker__c serExpRec : listToUpdate){
            
            serExpRec.Service_Expansion_Project_Status__c =  'New';     
        }
        
        update listToUpdate;
    }
    
}   
1
  • Hey, would you be able to help with this request?
    – Niraj Dev
    Oct 31, 2021 at 18:48

1 Answer 1

0

I can't determine what was the problem with the code you write that tried to set the status based on the status of the child record as you just shared the hardcoded version. However, here you have a working version for the code:

trigger ILM_ExpansionProjectStatusUpdate on Service_Expansion_Tracker__c(
  before insert,
  before update,
  after update,
  after insert,
  after delete,
  before delete
) {
  if (Trigger.isAfter) {
    if (Trigger.isUpdate) {
      updateRelatedNHCRecords(Trigger.new, Trigger.oldMap);
    }
  }

  private void updateRelatedNHCRecords(
    List<Service_Expansion_Tracker__c> expansionTrackerList,
    Map<Id, Service_Expansion_Tracker__c> expansionTrackerMapOld
  ) {
    Map<String, String> fieldNamesMap = new Map<String, String>{
      'Project_Status__c' => 'Service_Expansion_Project_Status__c'
    };

    List<NHC_Tracker__c> listToUpdate = new List<NHC_Tracker__c>();

    for (Service_Expansion_Tracker__c trackerObj : expansionTrackerList) {
      for (String fieldName : fieldNamesMap.keySet()) {
        Boolean needsUpdate = false;
        NHC_Tracker__c parentTrackerRecord = new NHC_Tracker__c(
          Id = trackerObj.NHC_Tracker__c
        );

        if (
          trackerObj.get(fieldName) !=
          expansionTrackerMapOld.get(trackerObj.Id).get(fieldName)
        ) {
          parentTrackerRecord.put(
            fieldNamesMap.get(fieldName),
            trackerObj.get(fieldName)
          );
          needsUpdate = true;
        }
        if (needsUpdate) {
          listToUpdate.add(parentTrackerRecord);
        }
      }
    }

    update listToUpdate;
  }
}

A few comments:

  • I've tried to keep your idea about using a Set for listing all the fields that need to be mapped to the parent object so it is easy to extend it in the future. However and because the names of the fields can differ between both objects (as it is in your case), I've replaced it with a Map<String,String> to set the relation between field names
  • You should consider moving the logic out of the trigger as it is strongly discouraged to keep it there. You should use a helper class
  • Also as a final comment, you have other tools to achieve this behaviour like Flows or Process Builders that should be considered before creating a trigger
3
  • Thank you so much Oscar. In past i have been advised to use a method in a trigger itself. I will try to compile it in a helper class.
    – Niraj Dev
    Nov 1, 2021 at 0:22
  • I was able to run the code, but i would like to send an email to the the email address related to Account_Executive__C which is a user lookup on Service_Expansion_Tracker__c once the status is updated. Can i add to this trigger itself?
    – Niraj Dev
    Nov 1, 2021 at 0:24
  • Hi Niraj, Sure you can do it. You can check any example out there of how to send an email from Apex and you can invoke that code from your trigger. However and as I mentioned, consider using a Process Builder/Flow for this as it will be easier to implement (also it is the Salesforce advice for this cases) Nov 1, 2021 at 17:46

You must log in to answer this question.

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