I'm attempting to create a case trigger to enable auto-response rules to be kicked off when inserting a case via the API. However, my test class seems to be failing. Anyone have any ideas why this wouldn't work?

Here is my trigger:

trigger MyCaseAutoResponse on Case (after insert) {
if (trigger.isAfter && trigger.isInsert) {    
    List<Case> newlyInsertedCases = [SELECT Id From Case WHERE Id IN :trigger.new];

    Database.DMLOptions autoResponseOptions = new Database.DMLOptions();
    autoResponseOptions.EmailHeader.triggerAutoResponseEmail = true;

    for (Case c : newlyInsertedCases ) {
        Database.update(c, autoResponseOptions); 


And here is my test class:

private with sharing class MyCaseAutoResponseTest{
    private static void testCaseTriggerViaDML()
        Case testCase = new Case();
        insert testCase;

        Database.DMLOptions dmo = testCase.getOptions();

        System.assertNotEquals(null, dmo, 'DMLOption is null');
        System.assert(dmo.EmailHeader.triggerAutoResponseEmail, true);    


The first assert statement fails, since the DML options for the created Case record is null.

Any ideas?

  • Pretty sure auto response doesn't actually fire in a test context, at least the corresponding activity is not created. I've not tried using DMLOptions before though. – Girbot Jul 6 '15 at 16:00
  • I just reviewed how I tested/asserted it in the past, and I queried the Case after the insert and asserted that the Case owner had changed. I'm not 100% sure if that's the best way though. – Girbot Jul 6 '15 at 16:04
  • Thanks @Girbot. Nothing on the actual Case record is changing, so I'm not sure what I would test against. I was hoping the DML options would be there, but apparently they're not. – emroc Jul 6 '15 at 19:25

getOptions() won't return anything without a prior setOptions().

Your trigger is updating a completely different variable newlyInsertedCases than what is in Trigger.new[0] which is testCase- hence why testCase.getOptions() returns null

As for verifying that the autoresponse rules actually executed in the test, you'll need to verify something persistent like EmailMessage as a child of Case. Emails are never sent in testmethods but EmailMessage should exist.

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.