2

I have a junction object that links a meeting note to contacts who attended the meeting. It is a master-detail relationship.

How do I get the name of the linked contact too in Apex? It works in developer console query editor, but not in Apex.

This returns the contact names in Developer Console's query editor corectly:

SELECT Meeting_Note__c, Contact__c, Contact__r.Name 
FROM Contact_Note_Relation__c 
WHERE Meeting_Note__c = 'a000l00000ED5aJAAT'

However this Apex does not work correctly:

System.debug([
  SELECT Meeting_Note__c, Contact__c, Contact__r.Name
  FROM Contact_Note_Relation__c
  WHERE Meeting_Note__c = 'a000l00000ED5aJAAT']);

In Apex the name is not returned. I just get this:

Contact_Note_Relation__c:{
    Meeting_Note__c=a000l00000ED5aJAAT, 
    Contact__c=0030l00000WIAYBAA5, 
    Id=a060l000005TKgeAAG,
    CurrencyIsoCode=USD
}

FYI a060l000005TKgeAAG is the Id of the Contact Note Relation record.

EDIT: I was able to resolve it by adding a Contact Name formula field to the Contact Note Relation object, but I would rather understand how to do this without adding a formula field if possible.

3

system.debug() may or may not display the related data, but it is indeed there.

If you System.debug([SELECT Meeting_Note__c, Contact__c, Contact__r.Name FROM Contact_Note_Relation__c WHERE Meeting_Note__c = 'a000l00000ED5aJAAT'][0].Contact__r.Name); you should see the data you're looking for.

You could also have system.debug() print out the entire related record like so:

Contact_Note_Relation__c myRel = [
  SELECT Meeting_Note__c, Contact__c, Contact__r.Name
  FROM Contact_Note_Relation__c
  WHERE Meeting_Note__c = 'a000l00000ED5aJAAT'];

// Contact__r is an embedded SObject, and can be pretty much treated like any
//   other SObject record.
system.debug(myRel.Contact__r);
3
  • Well, more specifically, "will not" display related object (parent, child) data. JSON.serialize could be used here to see everything as well.
    – sfdcfox
    Oct 15 '20 at 15:44
  • I confirm that the data is there. The data structure returned is below. However it is quite difficult to extract this field due to errors from both Visual Studio and Apex (Apex gives me "Invalid field Contact__r.Name for Contact_Note_Relation__c" at runtime.) Thank you very much for your help, I will keep this in mind in the future. { "Meeting_Note__c":"a000l00000Dt4NQAAZ", "Contact__c":"0030l00000YfeWnAAJ", "Id":"a060l000005eSNcAAM", "Meeting_Note__r":{"Id":"a000l00000Dt4NQAAZ"}, "Contact__r":{"Name":"Timothy Flannagan","Id":"0030l00000YfeWnAAJ"} }
    – skycafe
    Oct 17 '20 at 8:40
  • Thanks for your help. I ran into a similar issue when attempting to find contact name and email via a TaskRelation. The name was available but email was not, even with JSON.Serialize, but Serialize helped me figure that out for sure.
    – skycafe
    Oct 23 '20 at 5:52

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.