You need to sign in to do that
Don't have an account?
Mark 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
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
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:
Please let me know if this helps.
Thanks,
Apoorv
All Answers
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
@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 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?
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:
Please let me know if this helps.
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