You need to sign in to do that
Don't have an account?
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?
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.
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?
"Child Relationship Name" is not "API name".
see below.
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.
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.