2

i'm looking for a solution and wondering if it's possible to send an email from auto response rule after a case update.

triggerAutoResponseEmail in a trigger after update is set to true but i receive nothing, when i use trigger after insert it works without probleme. (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Database_EmailHeader.htm)

I need to send email when a case is created and contact field is filled. But when a case is created without contact i need to wait until having email and others parameters to send response rule.

EDIT :

trigger AfterCaseUpdate on Case (after update) { 
    if(RecursiveTriggerHandler.isTriggered){ // Avoid update loop
        RecursiveTriggerHandler.isTriggered = false;
        List<Case> newlyInsertedCases = [SELECT Id From Case WHERE Id IN :trigger.new]; 
        Database.DMLOptions autoResponseOptions = new Database.DMLOptions(); 
        autoResponseOptions.EmailHeader.triggerAutoResponseEmail = true;
        autoResponseOptions.EmailHeader.triggerOtherEmail = true;
        for (Case c : newlyInsertedCases ) { 
            Database.update(c, autoResponseOptions); 
        } 
    }
}

To avoid infinite loop

public class RecursiveTriggerHandler{
     public static Boolean isTriggered = true;
}
2
  • Are you sending this email to an internal user, or an external user? Are you also setting triggerUserEmail/triggerOtherEmail (respectively)? Including the code you currently have (via an edit) is usually a good idea.
    – Derek F
    Feb 25, 2020 at 15:59
  • Here is the code. Users are outside of the organisation so i think triggerUserEmail is useless in this situation
    – Vneq
    Feb 25, 2020 at 16:24

1 Answer 1

3

It doesn't work on update

Tried this one for myself, and got a similar result (no email sent).

Based on the help page for Differences between Auto-Response Rules and Workflow Email Alerts, it appears that auto-response rules are only evaluated on record creation. Tables aren't really a thing on the stackexchange network, so I'll be taking some liberty in the formatting of my quotation.

  • TYPE OF PROCESS
    • Auto-response rules
  • DESIGNED FOR
    • Initial response to the contact who created a case or the person who submitted the lead on the web
  • RUNS WHEN
    • A case or lead is created
  • SENDS EMAIL TO
    • Contact on a case or the person who submitted the lead on the web
  • NUMBER OF EMAILS SENT
    • Sends an email based on the first rule entry criteria that the rule matches in a sequence of rule entries

The entry for Workflow Email Alerts says that it's run on creation or edit, so it feels reasonable to conclude that Auto-Response rules are not evaluated on update (given our results and the omission of "edited" in the "runs when" part of the help page).

Alternative

A workflow or process can probably be used here instead.

I'd imagine that you'd want to add a check to see that the email is changing from a null value to a non-null value (don't want to spam your contact on every update to the Case).

Having a field to indicate that an email has already been sent might also be a good idea, though this may not be strictly necessary (if a workflow is set to run when a record is subsequently edited to meet the criteria, and your criteria is "this email isn't null", then that may be sufficient).

Supplement

There are a few issues I see with your code that will cause problems, but not this problem. Just something to keep in mind in the future.

  • a simple static boolean recursion guard will mean it only runs for the first 200 records
    • If you update 300 records, then 100 of them will not go through this trigger
  • You have DML in a loop, which is bad
    • DML via the Database methods can take both a List and DMLOptions
  • If Auto-response rules were evaluated on update, you would end up sending a lot of emails because you only check that an email exists
    • Checking that the email is changing from null to non-null would be better
    • Having a field to say "hey, we've already sent an email for this case", and updating that as well, would be a good thing to consider
3
  • if you think about it, auto-response rules on Update would be prone to catastrophe such as executing a batchable or data loader across a corpus of Cases and flooding case contacts with unexpected emails. As there is no DmlOptions header to trigger auto-response rules, it would appear SFDC thought well about the implications.
    – cropredy
    Feb 25, 2020 at 22:56
  • I agree that the code can be improved, it was just to test and see if it works well before going further. Currently we have a workflow that sends emails with process builder, and there is a boolean field to check if the email was sent or not to avoid spamming our users.
    – Vneq
    Feb 26, 2020 at 10:23
  • Why did we want to use Auto-response rules? Because Auto-response rules automatically creates an activity in the history of the case, and we can see in the case the content of the email that was sent. Workflow send emails but there is no trace in the activity. We will look and verify if we can create the activity in another way in the history when we send mails with workflow. Thank you for your response and clear explanation.
    – Vneq
    Feb 26, 2020 at 10:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .