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
Stephanie_ArceStephanie_Arce 

SOQL relationship query

Hi, I'm trying to query on 3 objects with different relationships and need some help. The "base" of my query is a Receipt object, which has a lookup to a Gift object. The Gift object then has child records in an Allocation object. What  I'd like to do is query Receipt data while listing the Allocations associated with the Gift on the receipt.

I can query the Receipt and Gift data like so:
SELECT Id, causeview__Gift__r.Id FROM causeview__Receipt__c

And query the Allocation child records from the gift:
SELECT Id, (SELECT Id FROM causeview__Gift_Allocations__r) FROM causeview__Gift__c

But I'm not sure how to put the 2 together to get everything 1 query.

Thanks for any help!
Best Answer chosen by Stephanie_Arce
Martijn SchwarzerMartijn Schwarzer
Hi there,

You will need to go from the causeview__Gift__c object, since both the causeview__Receipt__c and the causeview__Gift_Allocations__c are child objects of causeview__Gift__c object.

your query should look something like this:

Select Name, Id, (Select Id, Name From causeview__Receipts__r), (Select Id, Name From causeview__Gift_Allocations__r) From causeview__Gift__c

Hope this helps!

Best regards,
Martijn Schwärzer

All Answers

AshlekhAshlekh
Hi,

According to you Gift object is parent of both objects ( Receipt and Gift Allocation ).

So you can find a gift record with childs reocrd by below query

SELECT Id, (select id form relationshipname_with_alloction_object ),(SELECT Id FROM causeview__Gift_Allocations__r) FROM causeview__Gift__c
Ramu_SFDCRamu_SFDC
A sample query goes something like this Select Id, Name, (Select Id, Name From Contacts) From Account where id in (select accountid from opportunity where name='gt')

Hope this helps !!
Martijn SchwarzerMartijn Schwarzer
Hi there,

You will need to go from the causeview__Gift__c object, since both the causeview__Receipt__c and the causeview__Gift_Allocations__c are child objects of causeview__Gift__c object.

your query should look something like this:

Select Name, Id, (Select Id, Name From causeview__Receipts__r), (Select Id, Name From causeview__Gift_Allocations__r) From causeview__Gift__c

Hope this helps!

Best regards,
Martijn Schwärzer
This was selected as the best answer
Harish1234Harish1234
hi

lets try this once...

[Select Name, Id, (Select Id, Name From causeview__Receipts__r), (Select Id, Name From causeview__Gift_Allocations__r) From causeview__Gift__c]
Stephanie_ArceStephanie_Arce
Thanks everyone! Martijn your explanation was especially helpful, I was thinking about it the wrong way. Our transaction object does have both Receipts and Allocations as child objects. This query works:

SELECT Id, (select id from causeview__Receipts__r ),(SELECT Id FROM causeview__Gift_Allocations__r) FROM causeview__Gift__c