You need to sign in to do that
Don't have an account?
falfadli
trouble getting primary contact info
I am trying to return some information for just the PRIMARY contact on a given account. I think I can do this in one query.
However the query below returns me all the contacts (primary and non primary). Can anyone tell me whats wrong with my SOQL and how I should go about just returning the details of the primary contact?
Select c.Phone, c.LastName, c.FirstName, (Select AccountId, ContactId From AccountContactRoles WHERE IsPrimary =true) From Contact c
Thanks
Or you could do it the shorter way:
Select a.Contact.FirstName, a.Contact.LastName, a.ContactId From AccountContactRole a WHERE a.IsPrimary =true and a.AccountId = '001Q0000002U1EB'
All Answers
I don't know if you can do it in one statement, but you can do it in two.
First, return the primary ContactId from the AccountContactRole table. Of course, you would have to provide the AccountId you are looking for.
Select ContactId From AccountContactRole WHERE IsPrimary =true and AccountId = <accountId>
Second, use the ContactId to get the Contact
Select c.Phone, c.LastName, c.FirstName From Contact c where c.id = <ContactId>
I tried doing those two statements as one, like you can do in SQL, but SOQL wouldn't do it.
You can do it in one:
Select c.Phone, c.LastName, c.FirstName From Contact c
where c.id IN
(Select ContactId From AccountContactRole WHERE IsPrimary =true and AccountId = '001Q0000002U1EB' )
Or you could do it the shorter way:
Select a.Contact.FirstName, a.Contact.LastName, a.ContactId From AccountContactRole a WHERE a.IsPrimary =true and a.AccountId = '001Q0000002U1EB'