function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shane KorosecShane Korosec 

Joining with SOQL

I have a simple joined soql query i'm using
SELECT Id, FirstName, LastName, Email, (SELECT AccountId, CreatedDate, ActivityDate, Subject, Description, ActivityType, CallType from ActivityHistories) FROM Contact WHERE Id = '".$id."'"

which works great for me. Now I want to get info from a custom object called 'Position__c', so i've added it to the selects
SELECT Id, FirstName, LastName, Email, (SELECT AccountId, CreatedDate, ActivityDate, Subject, Description, ActivityType, CallType from ActivityHistories), (SELECT Id, Position_title__c, End_Date__c FROM Position__c) FROM Contact WHERE Id = '".$id."'"

But I get an error with that, quoting 'Didn't understand relationship 'Position__c' in FROM part of query call.' I have also tried it with 'Position__r' also but get the same error.

Can anyone help me to run a successful query?

Ryosuke KobayashiRyosuke Kobayashi
Hi.

You have to use "Child Relationship Name + __r" instead of "Position__c(__r)"

You can know Child Relationship Name at lookup field detail page.
Please see below.

setting
└create
   └objects
     └Position
       └customField(Lookup(Contact))

Thanks.
Shane KorosecShane Korosec
Thanks Ryosuke, much appreciated

The custom lookup(contact) field API name is 'Assigned_Volunteer__c' so i've replaced 'Position__r' with 'Assigned_Volunteer__r' and i am still getting the same error: "Didn't understand relationship 'Assigned_Volunteer__r".

So the query now reads
SELECT Id, FirstName, LastName, Email, (SELECT AccountId, CreatedDate, ActivityDate, Subject, Description, ActivityType, CallType from ActivityHistories), (SELECT Id, Position_title__c, End_Date__c FROM Assigned_Volunteer__r) FROM Contact WHERE Id = '".$id."'"

Is there something else i'm overlooking?
Ryosuke KobayashiRyosuke Kobayashi
Hi Shane.

"Child Relationship Name" is not "API name".

see below.
User-added image
In this case , the query will be
SELECT Id, FirstName, LastName, Email, (SELECT AccountId, CreatedDate, ActivityDate, Subject, Description, ActivityType, CallType from ActivityHistories), (SELECT Id, Position_title__c, End_Date__c FROM Positions__r) FROM Contact WHERE Id = '".$id."'"

Thanks.
Shane KorosecShane Korosec

That explains it - i've been grabbing the wrong field. I've now been able to run a successful query!

Thanks Ryosuke, i appreciate your help with this.

Ryosuke KobayashiRyosuke Kobayashi
I'm glad that this was resolved:)