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
carmilyn.c martincarmilyn.c martin 

merge fields not working on @invokablemethod in Process builder

hello, 

We have a process builder created that will fire if a new record is created and the action is to create another Action and the to send an email. Since we need to cc the managers we created an @invocablemethod and incorporate the Email Template we created the email is being sent however, the merge fields are all blank. Here is our Apex Class: 

public class SendEmailToRep_CcDMs{

    @InvocableMethod
    public static void GetUserRoleId(List<Id> proposedTerritory)
    {   
        string tempName = 'DS_Proposing_NICM_Tgt_Account';
        List<PTMS_Users_and_Role__c> userManager = [SELECT Id,Name,Manager__c,User__c,Manager__r.Email,User__r.Email,User__r.Id 
                                                    FROM PTMS_Users_and_Role__c 
                                                    WHERE Id IN:proposedTerritory];
                                                    
        Id templateId; 
        templateId = [select id, name from EmailTemplate where developername = :tempName].id;                                            
        List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
        
                    for(PTMS_Users_and_Role__c ptmsUsers: userManager){
                            
                            
                                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                                    
                                    //Send email to Rep
                                    /*List<String> sendTo = new List<String>();
                                    sendTo.add(ptmsUsers.User__r.Email);
                                    mail.setToAddresses(sendTo); */
                                    
                                    mail.setSenderDisplayName('Vector Team');
                                    mail.setTargetObjectId(ptmsUsers.User__r.Id);
                                    mail.setTemplateId(templateId);
                                    mail.setSaveAsActivity(false);
    
                                    //cc Manager
                                    List<String> ccTo = new List<String>();
                                    ccTo.add(ptmsUsers.Manager__r.Email);
                                    mail.setCcAddresses(ccTo);
                                                                     
                                    mails.add(mail);
                    }
                    Messaging.sendEmail(mails);
    }//end of method
}//end of class

Please help. 

Thank you. 

 
Maharajan CMaharajan C

Hi Carmilyn,

In the Email Template if you refering the custom objects then please use the merge the fields from custom object.
mail.setWhatId(ptmsUsers.Id);

Or refer this below link may be it will help to you.
https://salesforce.stackexchange.com/questions/1242/passing-custom-string-into-a-salesforce-email-template

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj