Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

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.

share|improve this answer
    
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 Mar 4 at 11:32
    
@user668027 is this resolved your issue can you accept it with the checkmark under the vote buttons? –  ca_peterson Mar 4 at 17:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.