Skip to main content
Mark Mulholland (TTC) 님이 #Salesforce Developer에 질문했습니다
If I have a variable of a SObject and I want to check a field on the parent record, how is that written?

I assumed it was in the format of "variable name"."lookup relationship name"."field name"

so if the variable is "Contact1" and I want to see the Account Name through that variable I would write

String AccountName = Contact1.Account.name;

If it's with custom objects then I would write it as

Passenger1.Account__r.name

If I try to do a system.assert on that then the string is returned as Null as if there is no relationship, but I have a feeling I am writing it wrong

I tried looking up the correct format but I could find no resource that has a variable, it's always in a SOQL query so it would just be Account__r.name

Can someone help please
답변 5개
  1. 2016년 10월 3일 오전 11:43

    Hi Mark,

    I would expect this to always return null, regardless of context.

    Generally, unless you have explicitly queried for fields on a related record, a reference using the relationship name will return null or throw a 'field not queried' exception.

    In your case, if you want to refer to the field Booking1.Selling_Company__r.name, you would need to insert the Booking1 record, then query for that same record:

     

    insert Booking1;

    Booking1 = [Select id,name,Selling_Company__r.name from Booking__c where id=:Booking1.id];

    System.debug(Booking1.Selling_Company__r.name); // It should give you the desired output

    Please let me know if this helps.

    Thanks,

    Apoorv
0/9000