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
Brooks Johnson 6Brooks Johnson 6 

Test Class on a Campaign Member Status Update

Hello Friends,

I have a method that is called from my Campaign Member Trigger Handler.  If a Campaign member's status changes to "Opened" then  Email Opened__c field on the contact object should be incremented by one.  It is working in the UI. But I can not get my test classes to pass. It seems that no matter how I try to change the Campaign Member Status in the test my System.debug  messages read as "Status = Sent." Any help would be much appreciated. 

Here are the methods. 
public with sharing class CampaignMemberUtils {
	
	public static void   main(List<CampaignMember> newList, Map<Id, CampaignMember> oldList){
		//if the contact status has changed and is opened or clicked call score method
		
		Set<Id> contactIds  = new Set<Id>();
		
		for (CampaignMember c : newList){
			CampaignMember oldMember = oldList.get(c.Id);
			System.debug('Email status is :' + c.Status);
			if (c.Status == 'Opened' && oldMember.Status != 'Opened'){
				contactIds.add(c.ContactId);
			}
		}
		System.debug('contactsToProcess.Size() = ' + contactIds.size());
			List<Contact> contacts = getContacts(contactIds);
			updateContactOpenScores(contacts);
		
	}
	public static void updateContactOpenScores(List<Contact> contacts){
			System.debug('Number of contacts passed to update open score method = ' + contacts.size());
			List<Contact> contactsToUpdate = new List<Contact>();
			for(Contact c : contacts){
				c.Pardot_Emails_Opened__c  +=1;
				contactsToUpdate.add(c);
			}
			update contactsToUpdate;
		}
	public static List<Contact> getContacts(Set<Id> ids) {
		List<Contact> contacts = [
				SELECT Id, Pardot_Emails_Opened__c, Pardot_Links_Clicked__c
				FROM Contact
				WHERE Id = :ids
		];
		return contacts;
	}
}

And here is my test class 
@IsTest public static void singleRecordFound(){
		Account a = new Account(Name = 'Test Acc');
		insert a;
		Contact c = new Contact(LastName = 'Test Contact', AccountId = a.Id, Pardot_Emails_Opened__c = 0);
		insert c;
		Campaign campaign = new Campaign(Name = 'Test Campaign', IsActive = true);
		insert campaign;
		CampaignMemberStatus  status = new CampaignMemberStatus(CampaignId = campaign.Id, Label = 'Opened');
		insert status;
		CampaignMember campaignMember = new CampaignMember(CampaignId = campaign.Id, ContactId = c.Id);
		insert campaignMember;
		CampaignMember updatedCampaignMember = [SELECT Id, Status FROM CampaignMember LIMIT 1];
		updatedCampaignMember.Status = 'Opened';
		update campaignMember;
		Contact updatedContact = [SELECT Id, Pardot_Emails_Opened__c FROM Contact LIMIT 1];
		
		System.assertEquals(1, updatedContact.Pardot_Emails_Opened__c);
	}

 
Raj VakatiRaj Vakati
Try this
 
@IsTest public static void singleRecordFound(){
		Account a = new Account(Name = 'Test Acc');
		insert a;
		Contact c = new Contact(LastName = 'Test Contact', AccountId = a.Id, Pardot_Emails_Opened__c = 0);
		insert c;
		Campaign campaign = new Campaign(Name = 'Test Campaign', IsActive = true);
		insert campaign;
		CampaignMemberStatus  status = new CampaignMemberStatus(CampaignId = campaign.Id, Label = 'Opened');
		insert status;
		CampaignMember campaignMember = new CampaignMember(CampaignId = campaign.Id, ContactId = c.Id);
		updatedCampaignMember.Status = 'Opened';
		insert campaignMember;
		
		updatedCampaignMember.Status = 'Responded';
		update campaignMember;
	 
	}



 
Brooks Johnson 6Brooks Johnson 6
Thanks Raj, when I tried that in my test still failed. I will also need to run different methods depending on different Campaign statuses: Opened, Clicked, Unsubscribed etc..