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
Mark Mulholland 3Mark Mulholland 3 

very basic relationship question

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
Best Answer chosen by Mark Mulholland 3
Apoorv Saxena 4Apoorv Saxena 4

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

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Mark,

If 'Passenger1' is your custom object having lookup to Standard 'Account' object, then to access 'name' field of Account you would have to write like this :


Passenger1__r.Account.name

Hope this helps!


Thanks,
Apoorv
Mark Mulholland 3Mark Mulholland 3
Sorry guys, I was away from my computer for the last few days

@Soni sure. This is an excerpt of the code I am trying to execute. As I said the part that I can't get is retreiving the value of a field from a parent record, so this all a self contained test. It's not calling methods from other classes/triggers
@istest private class ThisIsATest_Tests {

    //Declare variables for records that will be used
    private static Account Agency1;
    
    private static Booking__c Booking1;
    
    public static void createRecords(){
        
        //get Account record Types
        map<string, id> mapRT = new map<string, id>();
        for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Account']){
            mapRT.put(RT.DeveloperName, RT.id);
        }

        //get Contact record Types
        map<string, id> ConRT = new map<string, id>();
        for(RecordType RT : [SELECT id, DeveloperName, Name, SobjectType FROM RecordType WHERE SobjectType = 'Booking__c']){
            ConRT.put(RT.DeveloperName, RT.id);
        }
        
        //insert Accounts to be used in test
        Agency1 = new Account(Name = 'Agency Account 1', IATA_Code__c = '12345678', RecordTypeId = mapRT.get('Selling_Company'));
        insert Agency1;        
        
        //insert Passenger to be used in test
        Booking1 = new Booking__c(name = 'Booking1', Selling_Company__c = Agency1.Id, RecordTypeId = conRT.get('Booking'));
        insert Booking1;  
    }
    
    @isTest
    static void callAPIForTestResults(){
        
        //create records
        createRecords();
                       
        system.assertEquals(Agency1.name, Booking1.Selling_Company__r.name);
        
    }
}
Account and Booking are linked by a custom lookup called Selling_Company__c

The code is MEANT to fail because the strings are not the same. However the test result should read
"expecting Agency Account 1, actual Booking1"

the actual result is "expecting Agency Account 1, actual Null"

So how is the lookup to the Name on the Account supposed to be phrased?
Mark Mulholland 3Mark Mulholland 3
Sorry, it's not meant to fail it's meant to pass. Both times it's looking at the same field
Apoorv Saxena 4Apoorv Saxena 4

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
This was selected as the best answer
Mark Mulholland 3Mark Mulholland 3
Thanks Apoorv,

I thought that because I assigned the values in that batch of Apex that I wouldn't need to query the values

Thanks very much for the help
Mark