1

Getting some odd results from a query in a before trigger context

My Account object has a lookup to a object called Region__c. Region__c has a lookup to Division__c.

When I try to query the Division from the Account, it instead only returns the region id:

List<Account> accs = [SELECT Region__r.Division__c 
   FROM Account WHERE Id IN :Trigger.NewMap.Keyset()];
System.debug(accs);

(Account:{Id=0014C000008cL2LQAU, Name=asdas, OwnerId=0053000000BPS4DAAX, Region__c=a0k30000000QnH3AAK})

The only thing I can think of is this is a permissions issue (still would seem like weird behavior), but I've checked Object, FLS, used without sharing, and not to mention I'm running as an admin user.

What am I missing?

Update:

If I query the Division directly from the Region it works fine:

for(Account acc : Trigger.new){
   System.debug([SELECT Division__c FROM Region__c WHERE Id = :acc.Region__c]);
}

(Region__c:{Division__c=a1W3A000001dfIGUAY, Id=a0k30000000QnH3AAK})

1
  • 2
    Try adding that field in debug.
    – Raul
    Commented Mar 8, 2017 at 7:31

1 Answer 1

3

When you debug the parent query you will get the id of the lookup only.

To get the vale of the child field you have to explicitly output it.

Region__r.Division__c

Another way is the getSObject method if you need the sObject

Region__c tmp = (Region__c)accs.getSObject('Region__r');
System.debug(tmp.Division__c);

Note you can only get field explicitly in the query you created the SObject from

1
  • I did not know that... The code isn't working as expected and after debugging I got stuck on the fact that it must have something to do with the value not being there. Thanks!
    – NSjonas
    Commented Mar 8, 2017 at 7:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .