0

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.

The way I have read the documentation suggestions I should update the member status with the text value not the campaign member status Id. It does work in the UI.

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;
}

}

Here is the test

@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);
}
2

You are updating the campaignMember record and not the one which you change the status of which is updatedCampaignMember.

Try:

@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 updatedCampaignMember;
    Contact updatedContact = [SELECT Id, Pardot_Emails_Opened__c FROM Contact LIMIT 1];

    System.assertEquals(1, updatedContact.Pardot_Emails_Opened__c);
}
|improve this answer|||||
  • 1
    Thanks Zach, been staring at the screen too long. I can't believe I missed that. – Brooks Johnson Feb 6 '19 at 21:27

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.