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
Katie KourtakisKatie Kourtakis 

Help with writing test class on trigger that adds contacts to a campaign

Hello, I wrote a trigger to add contacts to a campaign once a rollup field on their associated account is greater than or equal to $1800. I am in the process of writing a test class for my trigger but I always run into a failure when I test. Here is my trigger:

trigger addPatrontoCampaign on Account (after update) {

    //find campaign Id
    List<Campaign> patronCampaign = [SELECT ID 
                                     FROM Campaign 
                                     WHERE Name = 'Patron Circle Member 2019'
                                     LIMIT 1];
    //New Contact List
    List<Contact> householdMembers = new List<Contact>();
    
    //New Campaign members List
    List<CampaignMember> patronMemberstoCreate = new List<CampaignMember>();
    
    //Loop through accounts
    for(Account acct : Trigger.new) {
        
    //Define criteria
        if(acct.Patron_Donation__c >= 1800) {
    
    //find all contacts with same Account ID
            householdMembers = [SELECT ID
                                FROM Contact
                                WHERE AccountID = :acct.ID];
            
    //Loop though found contacts
            for(Contact con : householdMembers) {
                
    //Add each contact to campaign
                patronMemberstoCreate.add(new CampaignMember(CampaignID = patronCampaign[0].ID,
                                                       ContactId = con.ID));
    //Insert
                database.insert(patronMemberstoCreate, false);
            }
        }
    }
}

Here is my test class for when one contact is associated with an account with the patron donation field at $1800. (Should be added to campaign)

@isTest
private class addtoCampaignTest {
    @isTest static void addtoCampaign() {
        //Create new Contact
        Contact con = new Contact();
        con.FirstName = 'Mary';
        con.LastName = 'Potter';
        insert con;
        
        //Create new Account
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        //Create new Campaign
        Campaign Patron = new Campaign(
        Name= 'Patron Circle Member 2019',
        Status = 'Active'
        );
        insert Patron;
        
        //Associate new account to new contact
        con.AccountId = acc.id;
               
        //Set criteria for trigger to execute
        acc.patron_donation__c = 1800;
        
        System.assertEquals(1, Patron.NumberOfContacts);
              
    }
}

Any help would be appreciated! Thank you
Best Answer chosen by Katie Kourtakis
Maharajan CMaharajan C
Add a one more line befor the assert line:

Campaign Patroncamp = [ SELECT Id,NumberOfContacts FROM Campaign WHERE Id = : Patron.Id];
System.assertEquals(1, Patroncamp.NumberOfContacts);

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Katie,

Please try the below test class :

@isTest
private class addtoCampaignTest {

       static testMethod void addtoCampaign() {
    
        //Create new Campaign
        Campaign Patron = new Campaign(
        Name= 'Patron Circle Member 2019',
        Status = 'Active'
        );
        insert Patron;
    
        //Create new Account
        Account acc = new Account();
        acc.Name = 'Test Account';
        insert acc;
        
        //Create new Contact
        Contact con = new Contact();
        con.FirstName = 'Mary';
        con.LastName = 'Potter';
        con.AccountId = acc.id;
        insert con;
               
        //Set criteria for trigger to execute
        acc.patron_donation__c = 1800;
        update acc;
        
        System.assertEquals(1, Patron.NumberOfContacts);
              
    }
}


Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C




 
Katie KourtakisKatie Kourtakis
Hello Maharajan,

Thank you for your help. With the new code I was able to achieve 100% code coverage but my system.assertEquals still failed. 
Maharajan CMaharajan C
Add a one more line befor the assert line:

Campaign Patroncamp = [ SELECT Id,NumberOfContacts FROM Campaign WHERE Id = : Patron.Id];
System.assertEquals(1, Patroncamp.NumberOfContacts);

Thanks,
Maharajan.C
This was selected as the best answer
Katie KourtakisKatie Kourtakis
Thanks! That worked.