1

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,
Status__c,Priority__c,Description__c,City__c,Street__c,Zip__c,
(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. – Oles Malkov Mar 7 '16 at 15:18
1

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.

So

Select Id,Case__r.CaseNumber,Case__r.Description,Name,
Status__c,Priority__c,Description__c,City__c,Street__c,Zip__c,
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 '16 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. – Vigneshwaran G Mar 8 '16 at 10:44
  • Before double click also the value is [object Object]. – Ejaz Mar 8 '16 at 10:48
1

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){
            newCaseList.add(case);
            }
        }

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

        //add your thing to your list of objects
        wOCTList.add(thing);
    }
    //return your object list
    return wOCTList;
}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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