1

we have relationship between account and contact. I specially created a child object called Product to Account by using Lookup relationship.

Now Account have two child. But Contact and Product objects does not have any relation between them.

What my requirement is how to fetch Account and contact records which is related to Product by using Product object.

This scenario is exactly looking like Getting the records between objects which does not have relation, But this post does not have proper answer. Even i tried with the answer which the post has, it is giving only one child record but not other child related records.

please help me how to get this requirement?

  • 1
    I too have same requirement, go to this Link. It may help. – user40339 Jan 10 '17 at 12:08
  • @Ananth, your question has the solution by using apex collection. But can we get this scenario by using Single SOQL query...? – user31765 Jan 10 '17 at 12:22
  • Please, close this question, i got answer for this. Thank you each and every one for your support. – user31765 Jan 24 '17 at 7:10
0

I too have similar scenario,

Account is parent of Contact and Contact is having lookup relationship with Fulfillment.

For fetching the Account information from fulfillment, I used to query fulfillment as below

SELECT Id,contact__c,contact__r.AccountId FROM Fulfillment__c

Please let me know if it helps for you.

thanks Ranjeet

  • I didn't explain my question clearly because of my confusion. Please check my updated post and please update your answer. – user31765 Jan 10 '17 at 7:08
0

You can query down one level, or up 4 levels. Since Account is the common parent, if you want them in one query, you must query starting from the account, like this:

SELECT Name,
    (SELECT Name, ProductCode, Family FROM Products__r),
    (SELECT Name, Email FROM Contacts)
FROM Account
    WHERE Id = :someValue

If you want to get a list of accounts based on some product criteria, you can do that, too:

SELECT Name,
   ...
WHERE Id IN (SELECT Account__c FROM Product2 WHERE ...)
  • Using common parent we can fetch records, But i need to get account and contact records based on Product – user31765 Jan 10 '17 at 9:07
  • @Staalin As I said, you can use a semi-join to find products; just use the filter in the second ... in the second query example. – sfdcfox Jan 10 '17 at 9:14
  • could you provide an example please @sfdcfox.... – user31765 Jan 10 '17 at 9:17
  • @SantoshKumar What part didn't you get? How can I clarify my answer? – sfdcfox Jan 10 '17 at 20:32
0

Check this out - Select Id,(Select Id,Name from Contacts) from Account where Id in(Select AccountId from Product2 where Id := pid)

I could not test this as I don't have the same relationships created.

  • This query is returning account records. where as i need products records related to Account and contact – user31765 Jan 10 '17 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy