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
Palmira AngelovaPalmira Angelova 

How to write an SOQL query that looks up child to parent to child?

Hi friends,

I need to look up specific Contacts on Accounts, but only include Contacts on Accounts that have Opportunities WHERE Stage IN('X','Y','Z') i.e. what I think of as a child -> parent -> child lookup. I've been using workbench for this. 

I couldn't figure out a way to do this within the syntax, so instead I opted for Account -> Contacts WHERE Account -> Opportunities meets my criteria, which seemed to make sense to me. However, my query is getting an error telling me I can't have a child -> parent lookup in the where clause:
"MALFORMED_QUERY: 
AND Id IN(SELECT Account.Id FROM Opportunity WHERE
ERROR at Row:5:Column:24
The inner select field 'Account.Id' cannot have more than one level of relationships"

Here is my query:
SELECT Id,
       (SELECT Id FROM Contacts WHERE Send_Partner_Updates__c = TRUE)
FROM Account
WHERE Account.POS_Provider_s__c NOT IN('Toshiba ACE', 'Toshiba ACE (old, non-integrated)')
      AND Id IN(SELECT Account.Id FROM Opportunity WHERE Stage IN('6 - Deployed', '6 - Deploying', '6.5 - Fully Deployed'))

Does anyone have a way to get by this? When I tried "SELECT Id FROM Opportunity", removing the child->parent lookup within the Opportunity query, I get this error: "The selected field 'Id' in the subquery and the left operand field in the where expression in the outer query 'Id' should point to the same object type"...so I haven't been able to think of a way around these two limitations.

Thanks for your help!
Best Answer chosen by Palmira Angelova
jigarshahjigarshah
Palmira,

Change the Account.Id to AccountId in the subquery for Opportunity hence your updated Soql query will now become
SELECT Id,
    (SELECT Id FROM Contacts WHERE Send_Partner_Updates__c = TRUE)
FROM Account
WHERE Account.POS_Provider_s__c NOT IN('Toshiba ACE', 'Toshiba ACE (old, non-integrated)')
      AND Id IN (SELECT AccountId, Stage FROM Opportunity WHERE Stage IN('6 - Deployed', '6 - Deploying', '6.5 - Fully Deployed'))
Hope that helps.

All Answers

Palmira AngelovaPalmira Angelova
I'm under the impression from this documentation (https://developer.salesforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com) > "Left Inner Join" that the  FROM Account...WHERE Id IN (SELECT Id FROM Opportunity...) at least should work?
jigarshahjigarshah
Palmira,

Change the Account.Id to AccountId in the subquery for Opportunity hence your updated Soql query will now become
SELECT Id,
    (SELECT Id FROM Contacts WHERE Send_Partner_Updates__c = TRUE)
FROM Account
WHERE Account.POS_Provider_s__c NOT IN('Toshiba ACE', 'Toshiba ACE (old, non-integrated)')
      AND Id IN (SELECT AccountId, Stage FROM Opportunity WHERE Stage IN('6 - Deployed', '6 - Deploying', '6.5 - Fully Deployed'))
Hope that helps.
This was selected as the best answer
Palmira AngelovaPalmira Angelova
Thanks again Jigarshah! Next time I post if it's you that answers we'll be old friends ; )
jigarshahjigarshah
Palmira, I am glad I can be of assistance and help. And being friends you can be buy me a beer the next time we meet ;) Are you coming to Tahoe Dreaming 18? If yes, we should definitely meet up. Regards, Jigar Shah
Palmira AngelovaPalmira Angelova
I'm not planning on it at the moment, but if I make it over I'll be sure to let you know! Haha I'd be happy to buy you a beer, you have saved me so much time!
Kat Harrison 2Kat Harrison 2
Just wanted to say that in 2021 this was an extremely helpful resource for my solution.  

Thank you, @jigarshah for your query. 

For any whom it helps, here is basically my query as a second example, with a metaphor to apply to your situation:  We're starting at a child, up to the parent and down to a different record from the starting child's related list, mixing standard and custom objects.

Imagine you have a knife in your hand (or a record about a knife, Id = 'b4h4k00EKNIFEEIDx' haha).  You need to determine if this is a steak knife, chef knife or a butter knife.  The certain way to determine that is to know if your knife came from an Account (i.e. Silverware Drawer) that has a Fork in it.   This query would give you the information about any forks in the Account (Drawer) that you got the knife from. 
SELECT Name, Id, ( SELECT Account__c, Name, Type_Flatware__c FROM Silverwares__r WHERE Type_Flatware__c = 'Fork' ) FROM Account WHERE Id IN (SELECT Account__c FROM Silverware__c WHERE Id = 'b4h4k00EKNIFEEIDx')


 
james lorensjames lorens
THanks for this solution! I also have the same case.
james lorensjames lorens
Web design is very important to web developers from https://mlsdev.com/services/mobile-app-development. Websites that are not designed properly will not function properly. Websites that are not properly designed will not gather traffic, and visitors will not return to the sites to shop, read information, or even download items. In short, a poorly designed website will not generate any revenue, and it will not be able to lead visitors to a site's purpose.
Gwen Taylor 7Gwen Taylor 7
I have got same type of issue with it. (https://www.mycfavisit.org/)
Fred NeumannFred Neumann
Creation of the app (https://comboapp.com/services/product-development/how-much-does-it-cost-to-create-an-app) is really time consuming process. Thanks for sharing this info
Katrina KellyKatrina Kelly
Thank you Kat for your query.
I solved my issue with it, when I created an app in https://joinsoft.com/services/web-application-development/
hsd ashsd as
Sound good. I want to use this scirpt for my business (https://achievegroup.asia/2018/06/04/grow-your-business-through-manpower-staffing-services/) growth page that hosted on WordPress.
dfd sddfd sd
You did a great job to share this whole scirpt of algo program I want to test it for my real estate (https://www.cashforhouseatlanta.com/georgia/atlanta/quick-sale/) program.
maritza wymanmaritza wyman
i think its working

In this query, we are retrieving the Name field from the Account object and the LastName field from the child Contact object. We are querying all accounts where the Industry field is equal to 'Technology'.
This query follows a relationship path from Account to Contact, where Account is the parent object and (https://procreatemac.com/)is the child object. The (SELECT LastName FROM Contacts) part in the query represents the subquery that retrieves the related child records. (https://procreatemac.com/)
Please note that the actual field names and relationship names may vary based on your specific Salesforce org's schema.