1

I have the following object (hed__Course_Enrollment__c), with a relation to the Contact object:

enter image description here

For a given hed__Course_Enrollment__c Id, I want to show the first and last name of the contacts.

If I make the next query, I get the IDs of the contacts:

$query = "Select Id,hed__Contact__c FROM hed__Course_Enrollment__c WHERE hed__Course_Offering__c='a042o000013Z2tiAAC'";

How can I get the contact's first and last name on the same call? I've tried some querys but I get errors ...

Example 1:

$query = "Select h.Id,h.hed__Contact__c, c.FirstName, c.LastName FROM hed__Course_Enrollment__c, Contact c h WHERE h.hed__Course_Offering__c='a042o000013Z2tiAAC'";

Fatal error: Uncaught SoapFault exception: [sf:MALFORMED_QUERY] MALFORMED_QUERY: hed__Course_Enrollment__c, Contact c h WHERE h.hed__Course_Offering__c='a042o000013Z2tiAAC' ^ ERROR at Row:1:Column:96 unexpected token: 'c' in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\EUTA\portal\e\soapclient\SforceBaseClient.php:789 Stack trace: #0 C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\EUTA\portal\e\soapclient\SforceBaseClient.php(789): SoapClient->__call('query', Array) #1 C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\EUTA\portal\e\test.php(122): SforceBaseClient->query('Select h.Id,h.h...') #2 {main} thrown in ...

Example 2:

$query = "Select Id,hed__Contact__c, (SELECT FirstName, LastName FROM Contact) FROM hed__Course_Enrollment__c WHERE hed__Course_Offering__c='a042o000013Z2tiAAC'";

Fatal error: Uncaught SoapFault exception: [sf:INVALID_TYPE] INVALID_TYPE: (SELECT FirstName, LastName FROM Contact) FROM hed__Course_Enrollment__c ^ ERROR at Row:1:Column:61 Didn't understand relationship 'Contact' in FROM part of query call. 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. in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\EUTA\portal\e\soapclient\SforceBaseClient.php:789 Stack trace: #0 C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\EUTA\portal\e\soapclient\SforceBaseClient.php(789): SoapClient->__call('query', Array) #1 C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\EUTA\portal\e\test.php(122): SforceBaseClient->query('Select Id,hed...') #2 {main} thrown in ...

I'm doing something wrong in relationships ... but I don't know what it is.

Can anybody help me?

Thanks!!

1

Have you tried hed__Contact__r.FirstName, hed__Contact__r.LastName? Since this is a custom field, accessing the related record fields in a lookup is typically done by replacing the __c of the field name with __r. SOQL relationship documentation.

Your final query would look like:

$query = "SELECT Id, hed__Contact__c, hed__Contact__r.FirstName, hed__Contact__r.LastName FROM hed__Course_Enrollment__c WHERE hed__Course_Offering__c = 'a042o000013Z2tiAAC'";
  • 2
    Replacing __c with __r is done for custom relationship fields (when querying upwards in the hierarchy, i.e. from child -> parent). Doesn't matter if the child or parent objects are standard or custom (the criteria is that it's a custom relationship field). – Derek F Jan 7 at 13:37
  • Thanks for catching that, I will correct my answer. – nbrown Jan 7 at 13:39
  • Thanks for your answer. I had read about the __r, but I ignored how to use it correctly. With your example, the query worked and I learned the use of relationships in calls. Thank you very much again. – Fran Bedia Jan 7 at 16:36
1

This is expected behavior since you're not mentioning the Child Relationship name in your query. For the nested queries, you need to use Child Relationship name.

For e.g. to retrieve all contacts associated with Account in a single Query would look like this: SELECT Id, Name, (SELECT Id, lastName FROM Contacts) FROM Account

here Contacts is a Child Relationship name. Similarly there will be also some name associated with your Contact Lookup field.

Follow below steps:

  1. Click on Contact Field highlighted in your snapshot.
  2. You will find something called as Child Relationship Name. Use that name in your Child Query.

Another way to find out the Child Relationship Name would be look at the Schema of hed__Course_Enrollment__c. Refer this link for more details.

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.