0

This question may seem simple to anybody there. I've been searching for a way(to no avail) on how to query the related Opportunities in Account. This isn't the normal Opportunity related list.

So for example ACME account has 'Opportunities' related list which has 4 records and another related list named 'Related Opportunities' which has 50 records. I wanted to query those 50 'Related Opportunities' records. I'm not certain if this is a back end object but I can't seem to find it.

Any help would be appreciated.

  • can you share what you have tried? – glls Sep 5 '17 at 0:09
  • 1
    Hi, i got it now. It took a while for me to figure it out. I used [SELECT Id, Name from Opportunity where Account.Id = 'AccountId'] instead of [SELECT Id, Name from Opportunity where Distributor__c == 'AccountId']. So I should be using a different filter. My lesson learned, so there could be two account(or any other obj) lookup fields to Opportunity. – Loren Sep 5 '17 at 0:50
1

Freya

another option (assumming the relationship name from Account -> Opportunities is OpportunitiesAsDistributor__r)

Account a = [select Id, Name, (select Id, Name from OpportunitiesAsDistributor__r) 
  from Account where ID = :someAccountId limit 1]

and you can loop through the Opportunities by

for (Opportunity o : a.OpportunitiesAsDistributor__r) {
  // do work
}
|improve this answer|||||
0

opportunity and Account has a lookup relationship, that's why if we want to find related opportunity we have to use lookup field(Account) of opportunity. Here in an opportunity, you already know that the query should be like this.

[select name from Opportunity where AccountId=:acid]
|improve this answer|||||

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.