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
TsvetyTsvety 

Cross Object Field Update Trigger

I am trying to create a trigger that will update a field on a Job object every time when a field on Opportunity is changed.

The field on the Job object is an email field and is called "Account Manager email". It should be populated with the Opportunity Owner's email.
(I cannot use a simple formula field for the Account Manager email, because I want to use this field in a workflow)

Will appreciate it if someone can help me write the trigger.

Thanks!
Sunil PalSunil Pal
Hi Tsvety,

Before that may I know what is the relation between the Job Object and Opportuniy Object ?

Thanks
Sunil
TsvetyTsvety
Hi Sunil,
There is a lookup field on Job that pulls up data from Opportunity.
Sunil PalSunil Pal
HI Tsvety

Please try this and let us know
 
trigger UpdateJobObject on Opportunity (after Update) {

    updateJobObject objJobObject = new updateJobObject();
    
    if(Trigger.isAfter && Trigger.isUpdate){
        
        objJobObject.onAfterUpdate(Trigger.oldMap, Trigger.newMap);
    }
    
    
    
}
===========================================
public with sharing class updateJobObject{


    public void onAfterUpdate(Map<Id, Opportunity> mapNewOpportunity, Map<Id, Opportunity> mapOldOpportunity) {
    
        checkJobObject(mapNewOpportunity, mapOldOpportunity);
    }
    
    
    private void checkJobObject(Map<Id, Opportunity> mapNewOpportunity, Map<Id, Opportunity> mapOldOpportunity) {
        
        List<JOb__c> lstJobUpdate = new List<JOb__c>();
        Map<Id, Opportunity> mapOppId_Opp = new Map<Id, Opportunity>();
        
        Map<Id, String> mapOppId_Email = new Map<Id, String>();
        
        
        for(Opportunity objOppNew : mapNewOpportunity.values()) {
            
            system.debug('***8objOppNew***'+objOppNew.StageName);
            // For now I am checking the if the Stage is changed or not according to your condition you can check which field you want to check
            if(objOppNew.StageName != mapOldOpportunity.get(objOppNew.Id).StageName) {
            
               mapOppId_Opp.put(objOpp.Id, objOpp);
            }
        }
        
        for(Opportunity objOpportunity: [   SELECT Id, Name, Owner.Email, OwnerId 
                                            FROM Opportunity
                                            WHERE Id IN : mapOppId_Opp.keyset()
                                        ]) {
                                        
        
            mapOppId_Email.put(objOpportunity.Id, objOpportunity.Owner.Email);
        }
        
        
        for(JOb__c objJob : [   SELECT Id, Name, Account_Manager_email__c, Opportunity__c
                                FROM JOb__c 
                                WHERE Opportunity__c IN : mapOppId_Email.keySet()
                            ]) {
            
              objJob.Account_Manager_email__c =   mapOppId_Email.get(objJob.Opportunity__c); 
              lstJobUpdate.add(objJob);               
        }
        
        if(!lstJobUpdate.isEmpty()) {
            
            update lstJobUpdate;
        }
    }
    
}

Thanks
Sunil
TsvetyTsvety

Hi Sunil,

Thanks for taking the time to write the code.
I get the following error message "Error: Compile Error: Variable does not exist: objOpp.Id "