1

For a given account I need a list of all contacts and contact roles. For example: if account a has three contacts and two of them have contact roles:

Contact.Name    AccountContactRole.Role
A               Purchaser
B               Influencer
C               null

Is this possible in one query or do I need to query all contacts and all contact roles separately?

6

See the blog for SOQL subquery

You can use a inner query to achive this.

Try this code:

List<Account> accountList = [Select Id, Name,(Select id from AccountContactRoles), (Select Id from Contacts)   from Account where Id=: 'some id'];

System.debug('###'+accountList);

for(Account obj : accountList){
    System.debug(':::'+obj.Contacts);
    System.debug(':::'+obj.AccountContactRoles);
}
1

If I understand you correctly, this will do what you need.

SELECT Contact.name, Role FROM AccountContactRole WHERE Account.id = XXXX

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.