2

I am trying to write a Unit Test that passes an Apex trigger for inserting 2 custom CampaignMemberStatus records. The trigger is performing as expected in the Sandbox UI as an end user, however it does not seem to be firing when a a Unit Test is run and therefore the code coverage is below the needed 75%.

Starting off with the Test class:

@isTest
private class autoCampaignMemberStatusTrigger_Test {
    private static testMethod void myTest() {

        Campaign campaign_1 = new Campaign();
        campaign_1.Name = 'TestCampaign';
        insert campaign_1;

        //store campaign_1 Id

        Id camp1Id = campaign_1.Id;

        /* The insert trigger is *supposed* to fire upon campaign insert and
        insert 2 new CampaignMemberStatus records 
        with Labels of "Net New" and "Campaign Acquisition"   */


        //SOQL query for finding the 'New New' CampaignMemberStatus
        CampaignMemberStatus testCms1 = [SELECT Id, HasResponded 
        FROM CampaignMemberStatus 
        WHERE CampaignId = :camp1Id AND Label = 'Net New'];

        /* Now let's test! Assert that our testCms1 is inserted with 
        a 'HasResponded' value of false */
        System.assertEquals(testCms1.HasResponded, false);

        CampaignMemberStatus testCms2 = [SELECT Id, HasResponded 
        FROM CampaignMemberStatus 
        WHERE Id = :camp1Id AND Label = 'Campaign Acquisition'];

      /*Now let's test! Assert that our 
      testCms2 is inserted with a 'HasResponded' value of true */

      System.assertEquals(testCms2.HasResponded, true);                

    }
}

However, the "Run Test" returns this error:

System.QueryException: List has no rows for assignment to SObject Class.autoCampaignMemberStatusTrigger_Test.myTest: line 35, column 1

This suggests to me that the Apex trigger is not inserting new records, but how could I test this trigger otherwise?

And any idea why the trigger would fire just fine in the UI but not in a Unit Test?

Here's the Apex Trigger:

trigger autoCampaignMemberStatusTrigger on Campaign (after insert) {

    List<CampaignMemberStatus> cms = new List<CampaignMemberStatus>();
    Set<Id> camps = new Set<Id>();
    List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();

    for(Campaign camp : trigger.new){

            camps.add(camp.Id);
    }   


    for(CampaignMemberStatus cm : [select Id, Label, CampaignId from CampaignMemberStatus where CampaignId IN :camps]) {

            CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=false,
             Label = 'Net New', SortOrder = 3, isDefault = true);
             cms2Insert.add(cms1);          

            CampaignMemberStatus cms2 = new CampaignMemberStatus(CampaignId = cm.CampaignId, HasResponded=true,
             Label = 'Campaign Acquisition', SortOrder = 4);
             cms2Insert.add(cms2); 

    }


    insert cms2Insert;
}

1 Answer 1

4

Your trigger shouldn't be querying for campaign member status values. I suspect that, for whatever reason, the status values are not created at the point in time in which you expect them to be. Instead, just create the new status items directly:

trigger autoCampaignMemberStatusTrigger on Campaign (after insert) {
    List<CampaignMemberStatus> cms2Insert = new List<CampaignMemberStatus>();
    for(Campaign record: Trigger.new) {

            CampaignMemberStatus cms1 = new CampaignMemberStatus(CampaignId = record.Id, HasResponded=false,
             Label = 'Net New', SortOrder = 3, isDefault = true);
             cms2Insert.add(cms1);          

            CampaignMemberStatus cms2 = new CampaignMemberStatus(CampaignId = record.Id, HasResponded=true,
             Label = 'Campaign Acquisition', SortOrder = 4);
             cms2Insert.add(cms2); 
    }
    insert cms2Insert;
}

You should find that this works for both your unit test and the UI.

1
  • That did the trick! I am surprised it was that simple!
    – GPP
    Commented Jan 29, 2018 at 5:28

You must log in to answer this question.

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