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
Eric_ArizonaEric_Arizona 

path resolves but returns null in apex

Trying to get an email address via User through an object called Community_Visit__c.
InsideSalesperson__c is a lookup(User) field in my custome object.

for (Community_Visit__c CV : listCVs){
      System.debug('cv.is='+cv.InsideSalesperson__c);
      System.debug('cv.is.email='+cv.InsideSalesperson__r.email);
      mail.setReplyTo(CV.InsideSalesperson__r.Email); mail.setSenderDisplayName(CV.InsideSalesperson__r.Name);
}

the above code returns the following debug info.
cv.is=005i0000000jo3XAAQ
cv.is.email=null

if I run this SOQL query  in developer console
SELECT InsideSalesperson__c,InsideSalesperson__r.email,InsideSalesperson__r.name from Community_Visit__c where account__c = '001e000000C1FwE'
it returns 

InsideSalesperson__c = 005i0000000jo3XAAQ
InsideSalesperson__r.Email = mary.k@emaiaddress.com
InsideSalesperson__r.Name = Mary K

so why is it returning null in apex ?
FYI this code is called from a trigger

thanks, Eric J
Best Answer chosen by Eric_Arizona
AshlekhAshlekh
Hi,

In trigger you can not get the "InsideSalesperson__r.email" value from Trigger,new or Trigger.old  List. Because trigger only contains its record value not parent info. So you have to first collect the  "InsideSalesperson__c" id in set and then you have to query on User object whose id in set.  And then get each related data by Map.

If you provide you trigger code then I can help you more.

IF it helps you than please mark it as a solution and ENJOY APEX

All Answers

AshlekhAshlekh
Hi,

In trigger you can not get the "InsideSalesperson__r.email" value from Trigger,new or Trigger.old  List. Because trigger only contains its record value not parent info. So you have to first collect the  "InsideSalesperson__c" id in set and then you have to query on User object whose id in set.  And then get each related data by Map.

If you provide you trigger code then I can help you more.

IF it helps you than please mark it as a solution and ENJOY APEX
This was selected as the best answer
Eric_ArizonaEric_Arizona
I was thinking maybe that was the case... Good info to know.
Thanks,
AshlekhAshlekh

Thanks to accept my suggestion.
 
Enjoy Apex.