+ Start a Discussion
pigginsbpigginsb 

DMLOptions Assigns Case But Doesn't Send Email to Queue Members

We are using Apex to create Cases in a customer community, and we have found that email notifications are not being sent to queue members when the Case is successfully assigned to a queue via the default assignment rule. Everything we've seen says to set the DML Options as shown in the below code, and this exact pattern is working elsewhere for Leads.
public class CaseTest {
    public static void InsertTest(String value, String accountId, String contactId) {
    
        Case aNewCase = new Case(
            AccountId = anAccountId,
            ContactId = aContactId,
            Origin = 'Customer Community'
        );

        aNewCase.Reason = 'Other';
        aNewCase.Subject = value;
        aNewCase.Description = value;

        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule= true; // successfully assigns case
        dmo.EmailHeader.triggerAutoResponseEmail = true; // but no email goes to the contact
        dmo.EmailHeader.triggerUserEmail = true; // and no email goes to the queue members
            
        aNewCase.setOptions(dmo);

        insert aNewCase;
    }
}
Additionally, when we create cases via the Salesforce UI, they are assigned correctly and the emails go out as expected. By this we conclude that there is no issue with the queue, the email template, or the email addresses of the assigned users.

Has anyone else encountered this issue, or better yet resolved it?
 
Best Answer chosen by pigginsb
Naval Sharma4Naval Sharma4
Hi,

This is how I am doing.
 
insert aNewCase;   //Insert case first
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
dmo.EmailHeader.triggerUserEmail = true;
Database.update(aNewCase, dmo);  //update case with dml options

 

All Answers

Naval Sharma4Naval Sharma4
Hi,

Updated the code and check if this works for you.
  
public class CaseTest {
    public static void InsertTest(String value, String accountId, String contactId) {
    
        Case aNewCase = new Case(
            AccountId = anAccountId,
            ContactId = aContactId,
            Origin = 'Customer Community'
        );

        aNewCase.Reason = 'Other';
        aNewCase.Subject = value;
        aNewCase.Description = value;

        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.useDefaultRule= true; // successfully assigns case
        dmo.EmailHeader.triggerAutoResponseEmail = true; // but no email goes to the contact
        dmo.EmailHeader.triggerUserEmail = true; // and no email goes to the queue members
            
        //aNewCase.setOptions(dmo);
         Database.update(aNewCase, dmo);
        //insert aNewCase;
    }
}


 
pigginsbpigginsb
Thank you. We had tried that with Database.insert(), since it is a new case, and it didn't work for us.
Naval Sharma4Naval Sharma4
Hi,

This is how I am doing.
 
insert aNewCase;   //Insert case first
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
dmo.EmailHeader.triggerUserEmail = true;
Database.update(aNewCase, dmo);  //update case with dml options

 
This was selected as the best answer
pigginsbpigginsb
Thanks, inserting the case in advance then applying the dml options on update achieves the desired effect, though it shouldn't have come to that.
Naval Sharma4Naval Sharma4
I am glad it helped you. I had the same problem so came up with this approach.

Please mark this as solved.

Thanks,
Naval