I want to show accounts and contacts data of a particular case in a form. how can i query the same using soql query in salesforce. I have a custom object named Work_Order__c which is having a master-detail relationship with CASE object. I am trying to write a below mentioned query, but the inner query is giving error.

select Id,Case__r.CaseNumber,Case__r.Description,Name,
(select Case__r.Account.Name, Case__r.Contact.Name from Work_Orders__r)
from Work_Order__c where Id = 'a024B0000025L6G'
  • Why are you using this inner query? If you replace inner query(select Case__r.Account.Name, Case__r.Contact.Name from Work_Orders__r) with Case__r.Account.Name, Case__r.Contact.Name it should work. Mar 7, 2016 at 15:18

2 Answers 2


There is no need for inner query here. Because the Account and Contact details are present in Case object which is master of Work Order object.


Select Id,Case__r.CaseNumber,Case__r.Description,Name,
Case__r.Account.Name, Case__r.Contact.Name 
from Work_Order__c where Id = 'a024B0000025L6G'

will give you the Account and Contact details of Case to which your current Work Order is related to.

Hope it helps.

  • It works but Case__r.Account.Name, Case__r.Contact.Name value is [object Object] how can i get single record for the same.
    – Ejaz
    Mar 8, 2016 at 10:38
  • If you query in developer console and double click it, it will show object object only. But if you use it in apex you will get the values. Mar 8, 2016 at 10:44
  • Before double click also the value is [object Object].
    – Ejaz
    Mar 8, 2016 at 10:48

Here's a different angle. I recently learned about making custom object classes. Some of the details may be wrong because I don't know how all of your objects are set up, but hopefully this will be useful to you.

//declare your new class
public class WorkOrderCaseThing{
    public Work_Order__c theWorkOrder{get;set;}
    public list(Case) theCases{get;set;}

//create method which returns a list of objects of your new class
public List(WorkOrderCaseThing) getWorkOrderCaseThings{
    //the returning list
    public list(WorkOrderCaseThing) wOCTList = new list(WorkOrderCaseThing);

    //list of work orders you wish to display
    public list(Work_Order__c) workOrderList = new list(Work_Order__c);
    workOrderList = [SELECT Name, Case__c FROM Work_Order__c];

    //list of all cases
    list(Case__c) caseList = new List(Case);
    caseList = [SELECT Name, Work_Order__c FROM Case]

    //list of cases you will return
    list(Case__c) newCaseList = new List(Case);

    //now it's time to make some objects
    // for each work order in your list
    for (Work_Order__c workOrder : workOrderList){

        //make a new thing
        WorkOrderCaseThing thing = new WorkOrderCaseThing();

        //make current work order your new thing's theWorkOrder
        thing.theWorkOrder = workOrder;

        //create the list of cases related to your work order
        for (Case case : caseList){
            if (case.Work_Order__c == workOrder){

        //make that list the thing's theCases
        thing.theCases = newCaseList;

        //add your thing to your list of objects
    //return your object list
    return wOCTList;

Be sure to use lists or sets to avoid using soql queries inside of for loops.

You must log in to answer this question.

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