I am attempting to query a list of Contacts (associated to the account) & OpportunityContactRoles (if applicable). So when a user is on an Opportunity Record they are able to see a full list of contacts associated to the account & if there is an opportunityContactRole for that contact and opportunity.
I was able to query the appropriate records with the following:
SELECT Id, Name, Title, Email, Mobilephone,
(SELECT Id, Role, IsPrimary from OpportunityContactRoles WHERE OpportunityId = OPPORTUNITYID)
FROM Contact
WHERE AccountId = ACCOUNTID
When I run this directly into the query editor is works and returns the data as expected, but when I call this in an apex class it is only returning the Contact Information, not the additional OpportunityContactRole Information. I believe this is because I am referencing 'Contact' list in the Apex and the additional fields don't exist on that object. So I would have to create a custom method for this data set.
- Is the best way to return the combined data set with a custom method?
- If so, what would that look like?
- Is there a more efficient way to approach this?
- Change Contact to Object? Which works...
- Is there a best practice for this?
APEX:
@AuraEnabled(cacheable=true)
public static List<Contact> getOpportunityContacts(string accountId, string opportunityId) {
String account = accountId;
String opportunity = opportunityId;
return [
SELECT Id, Name, Title, Email, Mobilephone,
(SELECT Id, Role, IsPrimary from OpportunityContactRoles WHERE OpportunityId =:opportunity)
FROM Contact
WHERE AccountId =:account
];
}
}