I have logic in my custom code where I need to send an email to the custom approver and its delegated approver. But I am not able to get the email id directly in SOQL.

List<Account> lstAcc = [Select id, approver1__r.Email, approver1__r.DelegatedApproverId, approver1__r.DelegatedApprover.Email from Account];

Is it possible to get the delegated Approver email directly?


This is an interesting question. Еhere is an old idea Add DelegatedApprover.Name to User object. I can just guess, that this field was intended to be polymorphic and maybe was intended to reference groups as well. Let's wait for the community guru to have a look at it.

For now, you have to use 2 separate SOQL queries to get the needed info.

List<Account> lstAcc = [Select id, approver1__r.Email, approver1__r.DelegatedApproverId from Account];

Set<Id> delegatedApproverIds = new Set<Id>();
for(Account acct :lstAcc){

Map<Id, User> delegatedApprovers = new Map<Id, User>([
    SELECT Id, Name, Email
    FROM User
    WHERE Id IN :delegatedApproverIds

for(Account acct :lstAcc){
    // this is the email of delegated approver for approver 1
    String approver1DelegatedApproverEmail = delegatedApprovers.get(acct.approver1__r.DelegatedApproverId).Email;

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.