4

This is the data model in our implementation.

Parent : Case

Child : Case_Search_Worklog__c

The following code snippet tries to set the Case_Search_Worklog__c.COWnerEmail with the email from the lookup.

trigger CaseWorklogTrigger on Case_Search_Worklog__c (after insert,before update,before delete){
     if ( trigger.isAfter )
      {
            if ( trigger.isInsert )
            {

                Case_Search_Worklog__c caseWorkFlow=trigger.new[0].clone();
                caseWorkFlow.id=trigger.new[0].id;
                System.debug('~~~~~'+caseWorkflow.id);
                System.debug('~~~~~'+caseWorkFlow.Case__r.Case_Co_Owner__r.Email); //Line 1
                caseWorkFlow.COwnerEmail__c = caseWorkFlow.Case__r.Case_Co_Owner__r.Email;


                if(!caseWorkFlow.PNC_Internal__c && caseWorkFlow.CaseMSTicket__c != null && (caseWorkFlow.External_Work_Log_Id__c==null || caseWorkFlow.External_Work_Log_Id__c==''))
                {
                  CaseWorkLogSend.SendCaseWorklogId(caseWorkFlow.id); 
                  caseWorkFlow.Alpha_Integration_Status__c='Processing';
                  update caseWorkFlow  ;
                }

           }   //  isInsert

     }

     if(trigger.isBefore){
         if(trigger.isDelete){
             try{
             BingWorklogTriggerHandler.IsCaseClosed(trigger.old);
             }
             catch(Exception e){
                 trigger.old[0].addError(e.getMessage());
             }
         }
         if(trigger.isUpdate){
             CaseWorkLogSend.escalateCWLBeforeUpdt(trigger.new);
         }

     }

}

But in debug I am seeing as "null" in the line 1

Can someone shed my light on this ?

But I am getting actual email when I do the following.

Case_Search_Worklog__c l = [SELECT ID,Case__r.Case_Co_Owner__r.Email FROM Case_Search_Worklog__c WHERE ID = 'a3Km000020002xEEAQ'];
System.debug(l.Case__r.Case_Co_Owner__r.Email);

1 Answer 1

9

This is a common misunderstanding about what is in scope when a trigger executes

The trigger.new variable in CaseWorklogTrigger only has access to the fields in Case_Search_Worklog__c and not access to related lists or lookup fields

Your trigger needs to query for the related lists and lookup fields and stash in a map.

Map<ID,Case_Search_Worklog__c> cswlIdToCaseSearchWorkLog =
  new map<ID,Case_Search_Worklog__c> {[select id, Case__r.Case_Co_Owner__r.Email
                                          from Case_Search_worklog__c 
                                          where id IN: trigger.new]};

and then you can access the related fields. Note this won't work in a before insert as there are no id values in trigger.new

You must log in to answer this question.

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