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
Tom Guo 19Tom Guo 19 

SOQL Query - Ids and not Values

I am trying to get the Contact Name and Email of the Primary Opportunity Contact. I am having difficulty as what is being returned is the Id of the contact and no email. 

Could someone let me know what key piece I'm missing here? 
 
List<OpportunityContactRole> primaryContactInfo = [SELECT OpportunityId, Contact.Name, Contact.Email
                    FROM OpportunityContactRole
                    WHERE OpportunityId ='0063h00000551YKAAY' AND isPrimary = TRUE];

System.debug(primaryContactInfo[0].get((string)'OpportunityId'));
System.debug(primaryContactInfo[0].get((String)'ContactId'));

How would I expose Contact.Name and Contact.Email? 
Best Answer chosen by Tom Guo 19
Maharajan CMaharajan C
Hi Tom,

Try the below one:

System.debug( ' Email ==> ' + primaryContactInfo[0].getSObject('Contact').get('Email'));

Thanks,
Maharajan.C

All Answers

SUCHARITA MONDALSUCHARITA MONDAL

Hi Tom,

You are debugging 'ContactId' instead of Contact.Name. Try this as below
System.debug(primaryContactInfo[0].get((String)'Contact.Email'));

Share your thoughts!

Thanks,
Sucharita

 

Tom Guo 19Tom Guo 19
Hi Sucharita,

Thank you for your response. 

To provide a bit more detail, I did try what you suggested earlier but I received the following error: System.SObjectException: Invalid field Contact.Email for OpportunityContactRole. 

If I simply output primaryContactInfo, I get the following List: DEBUG|(OpportunityContactRole:{OpportunityId=0063h00000551YKAAY, ContactId=0033h000004QLkNAAW, Id=00K3h000000uCULEA2})

Hence I'm able to get the ContactId of the Contact I'm looking for. I have no idea why for example Contact.Email doesn't come up in any form from my query. Any insights?
Maharajan CMaharajan C
Hi Tom,

Try the below one:

System.debug( ' Email ==> ' + primaryContactInfo[0].getSObject('Contact').get('Email'));

Thanks,
Maharajan.C
This was selected as the best answer