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
Deanna Aaron 4Deanna Aaron 4 

How to perform a union on a SOQL query

I'm looking to perform a union non a SOQL query. How would I do this?
I'm basically trying to join/combine two queries.

My sample queries are:
SELECT Name, fferpcore__BillingDocument__c, fferpcore__ProductService__c, ffbc__ContractLineItem__c, Id
FROM fferpcore__BillingDocumentLineItem__c
WHERE Include_in_Rev_Rec__c =FALSE

SELECT fferpcore__Account__c, fferpcore__CustomerReference__c, fferpcore__DocumentDate__c, fferpcore__DocumentStatus__c, Total_Invoice__c, ffaci__CongaEmailStatus__c, Id FROM fferpcore__BillingDocument__c WHERE fferpcore__DocumentStatus__c = 'Complete' AND TPx_Special_Invoice__c=FALSE AND fferpcore__DocumentType__c ='Invoice' AND ffaci__CongaEmailStatus__c != 'Sent'
AnkaiahAnkaiah (Salesforce Developers) 
Hi Denna, 

Do you have any relationship between fferpcore__BillingDocumentLineItem__c & fferpcore__BillingDocument__c  objects? 

Regards,
Ankaiah Bandi

 
mukesh guptamukesh gupta
Hi Denna,

As per my understanding  fferpcore__BillingDocumentLineItem__c object have a lookup with  fferpcore__BillingDocument__c object ,

so here fferpcore__BillingDocument__c is parent and  fferpcore__BillingDocumentLineItem__c is child.


So if you want to fetch all childs from it's parent then use below query:-


 
List<fferpcore__BillingDocument__c> BillingDocument = [SELECT fferpcore__Account__c, 
(SELECT Name, fferpcore__BillingDocument__c, fferpcore__ProductService__c, ffbc__ContractLineItem__c, Id
FROM fferpcore__BillingDocumentLineItem__c // replace with your relation ship name
WHERE Include_in_Rev_Rec__c =FALSE),
fferpcore__CustomerReference__c, fferpcore__DocumentDate__c, fferpcore__DocumentStatus__c, Total_Invoice__c, ffaci__CongaEmailStatus__c, Id FROM fferpcore__BillingDocument__c WHERE fferpcore__DocumentStatus__c = 'Complete' AND TPx_Special_Invoice__c=FALSE AND fferpcore__DocumentType__c ='Invoice' AND ffaci__CongaEmailStatus__c != 'Sent'];

if you want to fetch related patrent information  query:-
 
List<fferpcore__BillingDocumentLineItem__c> BillingDocumentLineItem = [SELECT Name,fferpcore__BillingDocument__r.Name,fferpcore__BillingDocument__r.fferpcore__DocumentDate__c,fferpcore__BillingDocument__c, fferpcore__ProductService__c, ffbc__ContractLineItem__c, Id
FROM fferpcore__BillingDocumentLineItem__c // replace with your relation ship name
WHERE Include_in_Rev_Rec__c =FALSE];



if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Deanna Aaron 4Deanna Aaron 4

Thank you, Mukesh. Yes, in this scenario the objects have a parent-child relationship. In regards to Ankaiah's reply, we will have objects that do not have a parent child relationship.

For example:
Contract line item with no shipping address
Payment with an error message in the description field

In this example, there is a relationship between the Contract Line Item and Payment (Not Parent-Child)

The path would be:
Payment > Billing Document > Contract > Contract Line Item

asp04__Payment__c > ffasp__BillingDocument__c > ffbc__Contract__c > ffbc__ContractLineItem__c
How would I create a join the queueries?

SELECT Name, fferpcore__BillingDocument__c, fferpcore__ProductService__c, ffbc__ContractLineItem__c, Id
FROM asp04__Payment__c
WHERE Include_in_Rev_Rec__c =FALSE
---Next---
SELECT fferpcore__Account__c, fferpcore__CustomerReference__c, fferpcore__DocumentDate__c
FROM ffbc__ContractLineItem__c 
WHERE fferpcore__DocumentStatus__c = 'Complete' AND TPx_Special_Invoice__c=FALSE AND fferpcore__DocumentType__c ='Invoice'