wondering if anyone can help me with a query on opportunity that contains contact fields. i've tried a bunch of solutions but no luck yet.
Select count(Id), Account.Contact.SomeField from Opportunity group by Account.Contact.SomeField;
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communitywondering if anyone can help me with a query on opportunity that contains contact fields. i've tried a bunch of solutions but no luck yet.
Select count(Id), Account.Contact.SomeField from Opportunity group by Account.Contact.SomeField;
You will not be able to do it this way as there is no direct relationship between the Opportunity -> account -> and a single account.
You have two options:
Add a Contact lookup on the opportunity then you could:
Select count(Id), Contact__r.SomeField from Opportunity group by Contact__r.SomeField;
Get a list of contacts with the Opportunity.AccountID and do something with them:
Map<ID,Contact[]> accConMap = New Map<ID,Contact>();
Set<ID> oppAccIDs = New Set<ID>();
for(Opportunity o : SOMEOPPLIST){
oppAccIDs.add(o.AccountID);
}
for(Contact c : [Select AccountID, ID From Contact Where AccountID IN :oppAccIDs]){
if(!accConMap.containsKey(c.AccountID))
accConMap.put(c.AccountID,New Contact[]{});
accConMap.get(c.AccountID).add(c);
}
//Do Something