function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
falfadlifalfadli 

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

Best Answer chosen by Admin (Salesforce Developers) 
werewolfwerewolf

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

bubkirchnerbubkirchner

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.

 

bubkirchnerbubkirchner

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' )

werewolfwerewolf

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'

This was selected as the best answer
falfadlifalfadli
Got it working in one line similar to Werewolf's posting. Thanks for all of your respones!