3

I am trying to learn Querying sObject Relationships through the APEX workbook (http://www.salesforce.com/us/developer/docs/apex_workbook/apex_workbook.pdf ) I have 2 objects with a master relationship, in the booklet says that I can query the parent just by specifying the relationship, but for some odd reason the result is quite unexpected

list<Line_Item__c> s = [SELECT Name, Unit_Price__c,Merchandise__r.Price__c,Merchandise__r.Name, Merchandise__r.Description__c FROM Line_Item__c];
system.debug(s);

16:42:24:062 USER_DEBUG [11]|DEBUG|(Line_Item__c:{Name=2, Merchandise__c=a039000000APTh3AAH, Unit_Price__c=200.00, Id=a029000000BuFUuAAN}, Line_Item__c:{Name=1, Merchandise__c=a039000000APTh2AAH, Unit_Price__c=1.50, Id=a029000000BuKcNAAV}, Line_Item__c:{Name=1, Merchandise__c=a039000000APTh2AAH, Unit_Price__c=1.50, Id=a029000000BtuHCAAZ}

I am quering 5 fields but only obtaining 4, and 2 of them are ids when it should be text fields

1 Answer 1

6

When writing to the debug log system.debug does not include other SObject instances, like you'd get from relationship queries. For example, Account.name and Account.OwnerId would not be logged even if queried.

The data is there however, you just need a more verbose form of writing out the data, which JSON.serialize or JSON.serializePretty does quite well.

Changing your example to:

list<Line_Item__c> s = [SELECT Name, Unit_Price__c,Merchandise__r.Price__c,Merchandise__r.Name, Merchandise__r.Description__c FROM Line_Item__c];
system.debug(JSON.serializePretty(s));

You should see the related object's (Merchandise__r) fields.

2
  • Thank you so much, that is really interesting I also notice something interesting, after checking the json it brings both id from Merchandise__r and Line_Item__c
    – manza
    Commented Mar 4, 2014 at 11:32
  • @user668027 is this resolved your issue can you accept it with the checkmark under the vote buttons? Commented Mar 4, 2014 at 17:56

You must log in to answer this question.

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