1. Account
  2. Contact
  3. Related Account


  1. Lookup between Account and Contact
  2. Master detail between Account and Related Account object.

Now, if I check a checkbox "checkbox1__c" on the contact object then it will check the distance value in the "Distance__c" field on the Related_Account__c object if the value is less than 5 then a email will go to contact owner.

I am writing a trigger but stuck on fetching the fields by joining these three object.


Select Name,Parent_Account__c,Distance__c,(Select Id,Name,(Select OwnerId,Send_Mail_Check__c from Contacts) from Related_Accounts__r) from Related_Account__c

Please guide me on this.

1 Answer 1


You can query "down" one level to children, and "up" five levels to parents, grandparents, etc.

Therefore, you're going want to start from the account, and select the contact and related account objects. You will need to do some post-processing to line up the data. Your query will start off like this:

SELECT (SELECT Id FROM Contacts WHERE Id = :Trigger.new),
       (SELECT Distance__c FROM Related_Accounts__r)
FROM   Account
WHERE  Id IN (SELECT AccountId FROM Contact WHERE Id = :Trigger.new)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .