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
bakumbakum 

understanding relationship queries

Hi,

I have a custom object, Logistics_Event__c, which has a field which is a lookup to another custom object, Course__c. (I'm doing this with the PHP toolkit, by the way)

I want to query my Logistics Events and return fields from Course__c: Name and Version__c.

I've tried two ways with no luck:
SELECT
            l.Id,
            l.Start_Date__c,
            l.Number_of_Days__c,
            l.Location__c,
            Course__r.Name,
            Course__r.Version__c
            FROM Logistics_Event__c l

Gives me no error, but doesn't return any info.  There is not ALWAYS a course ID on the Logistics_Event__c record, but there is about 50% of the time.  I would have thought it would return nothing sometimes and the field data when there's data to return.

I also tried this way:
SELECT
            l.Id,
            l.Start_Date__c,
            l.Number_of_Days__c,
            l.Location__c,
            (SELECT Name, Version__c FROM Course__c)
            FROM Logistics_Event__c l

ERROR: Didn't understand relationship 'Course__c' in FROM part of query call

Can someone illuminate me?

Thanks!

-mb
SuperfellSuperfell
Your first version should work fine, can you expand on how its not working for you?
bakumbakum
You know what?  It is working, I was iterating through the results wrong. 

Thanks for your time.

-mb
bakumbakum
So it turns out my original question was moot...but I do have a real question in this vein...

So on my custom object, Logistics_Events__c, I have a field, Onsite_Logistics_Contact__c which is itself a lookup to Contact.  I also have another field on that object called Book_Delivery_Contact__c which is ALSO a lookup to Contact. In my query I need to return the Onsite Logistics Contact's name and the Book Delivery Contact's name.

My query (below) works and returns the results (also below) I need but I'm not sure how to parse them.  I see nothing in the result set that indicates which of the sobject->SOBJECT[x] is Onsite_Logisitcs_Contact__r, and which is Book_Delivery_Contact__r.  THey're both simply of type=Contact. 

How do I differentiate them?

~~~~~~~~~~~~~~~~~~
MY QUERY:
SELECT l.Id,
                l.Start_Date__c,
                Course__r.Name,
                Course__r.Version__c,
                Onsite_Logisitcs_Contact__r.Name,
                Book_Delivery_Contact__r.Name,
                FROM Logistics_Event__c l



RESULTS:
SObject Object
(
[type] => Logistics_Event__c
[fields] => SimpleXMLElement Object
(
[Start_Date__c] => 2008-01-28
)

[Id] => a0KR0000000IAweMAG
[sobjects] => Array
(
[0] => SObject Object
(
[type] => Course__c
[fields] => SimpleXMLElement Object
(
[Name] => MTC
[Version__c] => 1
)

)

[1] => SObject Object
(
[type] => Contact
[fields] => SimpleXMLElement Object
(
[Name] => Buddy Kelley
)

)

[2] => SObject Object
(
[type] => Contact
[fields] => SimpleXMLElement Object
(
[Name] => Mark Baker
)

)

)

)