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
maceWindumaceWindu 

Using SOQL to access lookup relationships in custom objects.

I'm trying to access a custom lookup relationship field using SOQL and I was testing different queries in the FORCE IDE for Eclipse. I'm not getting the result I want and I wanted know how I can get SOQL to return the 

actual field value. 

 

SELECT custom_objectBLookup__r.CustomField__c

FROM 

custom_objectA__c

 

and I get this 

custom_objectB__c 

 

but instead I want to access the actual CustomField value 

 

"value"

 

werewolfwerewolf
That's just an artifact of the Force.com IDE's query builder.  If you were to perform that query in Apex code, for example, the actual value would just be there.
werewolfwerewolf

Well, to be clear, if you have something like

 

ObjA__c a = [select ObjB__r.FieldB, Id from ObjA__c limit 1];

 

Then you'd still have to reference your field as

 

a.ObjB__r.FieldB

 

So there's one level of indirection there.  In that sense the IDE is displaying the information strictly correctly, showing the one level of indirection, but it can be misleading to make you think that you have to do some crazy machinations to get to the data itself when in fact it's quite simple.

maceWindumaceWindu

Well I'm still not getting the actual value even when I try it in Apex using the System Log so for example if I do something like this.

 

 

List<ObjA__c> aa = [SELECT ObjA__c.ObjB__r.FirstName__c FROM ObjA__c];

for(ObjA__c aaa: aa)
{
     System.debug(aaa);

 

I still get the following:

AnonymousBlock: line 5, column 6: ObjA__c:{ObjB__c=############, Id=################}

 

yeah I agree it should be quite simple but its not and I'm not sure what to do with it.