3

I'm working on an Opportunity trigger and trying to pull the ID of a Contact that's connected via a lookup field on the Opportunity object called "Inbound_Contact__c". However, when this gets triggered on an Opportunity that has this lookup field filled out, the following line still outputs "NULL":

System.debug(opp.Inbound_Contact__r.Id);

Am I missing something on how to access the Id of this Contact? Thanks!

6

Don't use a relationship before you query it. For just the ID of a record, use the normal __c version of the field:

System.debug(opp.Inbound_Contact__c);

To use a __r (relationship), you have to query it first. In that case, you'd do so if you wanted extra details, like the email or phone number of the contact:

Map<Id, Opportunity> opps = new Map<Id, Opportunity>([SELECT Inbound_Contact__r.Email FROM Opportunity WHERE Id IN :Trigger.new]);

This is generally less efficient than querying the contacts directly, if that were your intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.