0

In my org, the User object has a lookup field to a Contact. In my Apex class, I need to return a data set that includes Contacts and the User Id associated with each Contact. The query below returns the Contacts I need:

Select Id, Name, LastName, FirstName, Account.Name, Account.ccrz__E_AccountGroup__r.Name from Contact where Account.ccrz__E_AccountGroup__c != null 
order by FirstName

All I need now is to return the User Object SFID for each Contact. Is there a way I can include the associated User object Id in the above query?

2

It would be easier to have your base query be the User instead of contact.

Select Id, Contact.Id, Contact.Name, Contact.LastName, Contact.FirstName, Contact.Account.Name, Contact.Account.ccrz__E_AccountGroup__r.Name 
from User 
where Contact.Account.ccrz__E_AccountGroup__c != null 
order by Contact.FirstName
1
1

The relationship is most easily handled from the User end:

SELECT ContactId, Contact.Name, Contact.LastName, Contact.FirstName, Contact.Account.Name, Contact.Account.ccrz__E_AccountGroup__r.Name FROM User WHERE Contact.Account.ccrz__E_AccountGroup__c != NULL ORDER BY Contact.FirstName

You then just need to use the collection of ContactId values from the results.

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.