I'm using Case object and it has a look up of Service_Technician__c. Now i want to get the Service technician name which is related to a Case. How can i retrieve a service technician name using SOQL? I tried like below:

sforce.connection.query("SELECT Id, CaseNumber, Reason, Status, (SELECT Id, Name FROM Service_Technician__c WHERE id= '"+account.Assigned_To__c+"'),Service_Date__c FROM Case WHERE Status='New'",{
    onSuccess : layoutResults,
    onFailure : queryFailed,
    source : {
        output : document.getElementById("output"),
        startTime : new Date().getTime()

Assigned_To__c is a look up name in Case Object

  • 1
    You could just put a formula field on the case to pull in the tech name – Eric May 19 '17 at 15:51
  • @Eric Tried using formula field. It's working as i expected – sfdcLearner May 22 '17 at 7:04

What you are doing right now (i.e. using a sub-query) would work for querying child-relations. But in this case you're following a lookup and not querying a child-relation at all.

In the case of querying a lookup field, you can simply use the __r version of the fieldname to query fields from that relation. So in this case use Assigned_To__r.Name to query the Name of the Technician.

SELECT Id, Assigned_To__r.Name FROM Case WHERE Status='New'

See the documentation for more information.

  • I am getting the following error.. SELECT Service_Technician__r.Name FROM ^ ERROR at Row:1:Column:8 Didn't understand relationship 'Service_Technician__r' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. – sfdcLearner May 19 '17 at 13:17
  • Then Service_Technician__c is not a lookup field. You said it is, right? – Willem Mulder May 19 '17 at 13:40
  • Assigned_To__c is a field in Case object, it is a look up of Service_Technician__c – sfdcLearner May 22 '17 at 6:02
  • Got it. It should be Assigned_To__r.Name – sfdcLearner May 22 '17 at 6:36
  • Ah ok, so the lookup field is Assigned_To__c and it references the Service_Technician__c object. I've updated my answer to reflect that! If it worked, please accept this answer or if it didn't: write your own and accept that one :-) – Willem Mulder May 22 '17 at 8:15

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.