2

I Invoked Apex class in process builder schedule actions. I passed the email template id in apex class then sending emails. I am having this problem with process builder schedule actions. The problem is, I passed dynamic field values in Email template, those field values are not displaying.But I created Time based workflow for this and has given same template. I'm receiving emails with proper field values. What could be the problem with invoking apex class with process builder?

Here you can find Process Builder schedule actions and APEX classes:

enter image description here

enter image description here

  public class TrainingExpiring30Days {
    @InvocableMethod
    public static void sendEmail(List<Certification__c> certif) {
    String sourceAccountId = String.valueOf(certif[0].Id);
    List<Messaging.SingleEmailMessage> emailsToSend = new List<Messaging.SingleEmailMessage>();
 Certification__c cert = [select id,Account__c from Certification__c Where id = :sourceAccountId];


    List<Contact> contactsToEmail = [SELECT Id
                                     FROM   Contact
                                     WHERE  AccountId = :cert.Account__c AND CPP_Access__c INCLUDES ('Receive Training Expiry Notices')];


    Id templateId = [SELECT Id 
                     FROM   EmailTemplate 
                     WHERE  Name = 'CPP: Training expiring 30 days'].Id;

    string SenderName = UserInfo.getUserName();        

    system.debug('Sender Name is:'+ SenderName);        

    for(Contact con: contactsToEmail ){

      Messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
      email.setTemplateId(templateId);
      email.setTargetObjectId(con.Id);
      email.setSenderDisplayName(Es); 
      mail.setWhatId(cert.id);
      emailsToSend.add(email);

    }
    Messaging.sendEmail(emailsToSend);  
}
}
  • show your process builder schedule actions in detail please. – cropredy Mar 18 '16 at 17:22
  • Neymar -- please show a fragment of your email template definition - especially the merge fields, and, if a VF email template, the definition of relatedTo – cropredy Mar 19 '16 at 15:06
  • Please refer this link. This is the exact error I'm facing. salesforce.stackexchange.com/questions/113839/…. I appreciate your concern . Please let me know if you need more information. – Neymar Da Silva Mar 19 '16 at 15:32
6

Neymar

In your email template, you are referring to merge fields as such:

{!Certification__c.somefld__c}

Yet, when you invoke the sendEmail(..) method in your invocableMethod, you need to call setWhatId(someCertificationId) in order to tell the template what object to use for the merge fields.

The email template, depending on how it is invoked has an implied whatid.

  • When referenced by a workflow/process builder action, it is implicitly, the object being evaluated by the workflow/process
  • When referenced from a Send Email button on the Activity related list, it is the parent object of that Activity
  • When referenced by an Apex sendEmail(..), you have to tell the email renderer (one of the things that sendEmail(..) does on your behalf) what object to use for the merge fields. If there is no whatId, the merge fields {!Certification__c...} resolve to null and hence don't display

You will either have to use a different email template or figure out a way to pass/derive a Certification__c ID in the invocableMethod.

  • Is there any way to pass two parameters in @Invocable method. I'm trying to pass one more parameter but, It is firing "Error: Compile Error: Only one parameter is supported on invocable methods at line" – Neymar Da Silva Mar 20 '16 at 15:13
  • @NeymarDaSilva. Since your process operates on Certifications, why not pass the Certification Id to the invocableMethod and fetch the account in apex? – cropredy Mar 20 '16 at 16:09
  • Thank you very much @cropredy. Now working fine, But duplicated emails are receiving. ForEx: If i schedule one action, I'm receiving two emails where i should receive only one email. Can you please suggest me how to avoid duplicate emails in process builder too. – Neymar Da Silva Mar 20 '16 at 19:31
  • @NeymarDaSilva - post this new issue as a separate question and include your Process Flow and specifics about the duplicate emails – cropredy Mar 20 '16 at 19:54

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.