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
Vittorio Pio RongaVittorio Pio Ronga 

Assignment Rules Email not triggered from Apex Code

Someone could help me with the Assignment Rules triggered by Apex Code?
I am able to use the Assignment Rule using the new Case button checking the dedicated field in the Page Layout.

Here the Rule Configured

Rule Configured

I am also able to trigger the rule using the trigger on the Case on After Insert using the following Code:

public void OnAfterInsert(Case[] newCase, Map<Id, Case> newCaseMap) {
        List<Id> caseIdSet = new List<Id>();
        for(Case c : newCase) {
            caseIdSet.add(c.Id);
        }
        
        //updateOwnership(caseIdSet);
        
           List<Case> caseList = new List<Case>();
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.AssignmentRuleHeader.useDefaultRule = true;
        
        
        for(Case c : [SELECT Id FROM Case WHERE Id IN: caseIdSet]) {
            c.setOptions(dmo);
            caseList.add(c);
        }
        update caseList;
    }

Unfortunatelly the Email Notification isn't sent when I use the trigger. Could you help me please?
If I use the following code I don't receive any email :-(

dmo.EmailHeader.triggerAutoResponseEmail = true;

Do you have any Idea?
Best Answer chosen by Vittorio Pio Ronga
Wilfredo Morillo 20Wilfredo Morillo 20
Found This: 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Database_EmailHeader.htm#apex_Database_EmailHeader_triggerUserEmail

You need to set to true the TriggerUserEmail : 
dmo.EmailHeader.TriggerUserEmail = true;

 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
Are you sure emailing is enabled in your org?  Check your setup.
Wilfredo Morillo 20Wilfredo Morillo 20
Found This: 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_Database_EmailHeader.htm#apex_Database_EmailHeader_triggerUserEmail

You need to set to true the TriggerUserEmail : 
dmo.EmailHeader.TriggerUserEmail = true;

 
This was selected as the best answer
Vittorio Pio RongaVittorio Pio Ronga
Thank you Wilfredo!! The command 'dmo.EmailHeader.triggerAutoResponseEmail = true;' doesn't work for this requirement and I have used what you suggested!!
Using the following code in my Trigger it works

public void OnAfterInsert(Case[] newCase, Map<Id, Case> newCaseMap) {
        List<Id> caseIdSet = new List<Id>();
        for(Case c : newCase) {
            caseIdSet.add(c.Id);
        }
        
        //updateOwnership(caseIdSet);
        
           List<Case> caseList = new List<Case>();

        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.AssignmentRuleHeader.useDefaultRule = true;
        dmo.EmailHeader.TriggerUserEmail = true;
        
        for(Case c : [SELECT Id FROM Case WHERE Id IN: caseIdSet]) {
            c.setOptions(dmo);
            caseList.add(c);
        }
        update caseList;
    }