+ Start a Discussion
Heshan Weerasekera 5Heshan Weerasekera 5 

System.AssertException: pls help

Hi All- with the help of this group, I wrote the below trigger and test class. The trigger is run on the camp members object. it calculates the unique members whose accounts are tagged as SQA. For example a campaign can have 50 members for 10 accounts. 5 of these accounts are tagged as SQA and I want that count of 5 shown on the Campaign. The test class passed in SB and I got a code coverage of 76%. when I tried to push to prod I get the error below

System.AssertException: Assertion Failed: Expected: 1, Actual: null 
Stack Trace: Class.UniqueSQAsTest.testCampaignWithSQA: line 54, column 1

my trigger and class below. Any help how to fix will be much appreciated 

Trigger
 
trigger UniqueSQAs on CampaignMember (after Insert, after Update, after Delete, after UnDelete) {
    List<Id> campaignIds = new List<Id>();
 
    if(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete) {
        for(CampaignMember member : Trigger.New) {
            if(member.CampaignId != null && member.ContactId != null && member.Account_ID__c != null && member.SQA_Org__c  == true) {
                campaignIds.add(member.CampaignId);
            }
        }
    }
    
    if(Trigger.IsDelete) {
        for(CampaignMember member : Trigger.Old){
            If(member.CampaignId != null && member.ContactId != null && member.Account_ID__c != null && member.SQA_Org__c  == true) {
                campaignIds.add(member.CampaignId);
            }
        }
    }
 
    // Get a list of Campaigns
    Map<ID,Campaign> campaignsOriginalMap = new Map<ID, Campaign>([SELECT ID, Sum_of_unique_SQAs__c FROM Campaign WHERE Id = :campaignIds]);
    
    // Get a list of CampaignMembers
    List<CampaignMember> campaignMembersLst = [SELECT Id, Account_ID__c, ContactId,CampaignId FROM CampaignMember WHERE CampaignId = :campaignIds AND SQA_Org__c  = true];
    
    // Loop round and remove elements that don't has a ContactId or Account_Id__c populated
    for(Integer i=0; i < campaignMembersLst.size(); i++){
         if(campaignMembersLst.get(i).ContactId == null || campaignMembersLst.get(i).Account_ID__c == null) {
             campaignMembersLst.remove(i);
         }
     }
    
    // Duplicate Account removal...
    Map<Id,CampaignMember> accountIdToCampMemberMap = new Map<Id,CampaignMember>();
    for(CampaignMember memRec : campaignMembersLst){
        if(!accountIdToCampMemberMap.containsKey(memRec.Account_ID__c)){
            accountIdToCampMemberMap.put(memRec.Account_ID__c,memRec);
        }
    }
    
    //Create a map of campaigns to be updated
    Map<Id,Campaign> campaignsToBeUpdatedMap = new Map<Id,Campaign>();
   
    //Iterate through the unique campaign members and add up the unique_SQAs to corresponding campaign
    for(CampaignMember membRec: accountIdToCampMemberMap.values()){
        if(campaignsToBeUpdatedMap.containsKey(membRec.CampaignId)){
            Campaign campaignRec = campaignsToBeUpdatedMap.get(membRec.CampaignId);
            campaignRec.Sum_of_unique_SQAs__c = campaignRec.Sum_of_unique_SQAs__c + 1;
        }else{
            Campaign campaignRec = campaignsOriginalMap.get(membRec.CampaignId);
            campaignRec.Sum_of_unique_SQAs__c = 1;
            campaignsToBeUpdatedMap.put(membRec.CampaignId,campaignRec);
        }
       
    }
   
    try {
        if(campaignsToBeUpdatedMap.size() > 0) {
            update campaignsToBeUpdatedMap.values();
        }
    }
    catch(Exception e) {
        system.debug('Thrown Exception for TotalDifferentCompaniesAttached Is: ' + e.getMessage());
    }
}
 
 
Test
 
@isTest
private class UniqueSQAsTest {
   private static testMethod void testCampaignWithSQA() {
    system.debug('Started Member Insert Test Class');
 
    //Start: Test Data Creation
    //Is there any common test data creation class??
   
    //Insert Account
    Account acct = new Account (Name = 'Acme, Inc.',
                                Website ='www.test.com',
                                Type = 'Other',
                                SQA_Categorisation__c='SQA',
                                Region__c = 'APAC',
                                Sub_Region__c ='India');
    insert acct;
    system.debug('Inserted Account, ID: ' + acct.id);
   
    //Insert Contact
    Contact con = new Contact(
                      FirstName = 'Robin',
                      LastName = 'Koehler',
                      AccountId = acct.Id,
                      Title ='COO',
                      Email ='robin@hotmail.com',
                      Phone ='5544789',
                      LeadSource ='Event',
                      contact_type__c='CCM');
    insert con;  
    system.debug('Inserted Contact, ID: ' + con.id);
 
    //Insert Campaign  
    Campaign camp = new Campaign(
                        Name = 'Test',
                        IsActive = TRUE
                        );           
    insert camp;
    system.debug('Inserted Campaign, ID: ' + camp.id);
   
    //Insert CampaignMember  
    CampaignMember member = new CampaignMember(
        ContactId = con.Id,
        Status = 'Sent',
        CampaignId = camp.Id
        );
    insert member;
   
    system.debug('Inserted CampaignMember, ID: ' + member.Id);
   
    Test.startTest();
   
 List<Campaign> campaigns = [SELECT Sum_of_unique_SQAs__c FROM Campaign WHERE Id = :camp.Id];
 
  system.assertEquals(1,campaigns.get(0).Sum_of_unique_SQAs__c);
 
    Test.stopTest();
    //End: Test Data Creation
 
 
   }
}
 
Paul S.Paul S.
On the inserted campaign member, you have to set the SQA_Org__c field to true and populate the Account_ID__c field.  Both of these are required for the trigger to do anything with the inserted record per this line:
if(member.CampaignId != null && member.ContactId != null && member.Account_ID__c != null && member.SQA_Org__c  == true)
{
    campaignIds.add(member.CampaignId);
}