0

I have a class that increments a custom field on the Contact record. If the contact has a campaign member status of Opened then the Pardot_Emails_Opened__c field increases by 1. If the status is Clicked then the Pardot_Emails_Opened__c field and the Pardot_Links_Clicked__c field should both be incremented.

The method to update the Email opens is working fine. But the click method is not doing anything. I am a little stumped as they are basically the same code. It does not work in the UI or in my test classes. My code coverage and System.debug statements show that nothing is being returned from the getContacts() method.

Any help teaching me why one is working and one is not would be appreciated.

public without 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> contactOpenIds  = new Set<Id>();
        Set<Id> contactClickedIds = 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'){
                contactOpenIds.add(c.ContactId);
            }if(c.Status == 'Clicked' && oldMember.Status != 'Clicked'){
                System.debug('Current member status = ' + c.Status);
                System.debug('Old Member status = ' + oldMember.Status);
                contactClickedIds.add(c.Id);
                System.debug('contactClickedIds.size() ' + contactClickedIds.size());
            }
        }
        if(!contactOpenIds.isEmpty()){
            List<Contact> contacts = getContacts(contactOpenIds);
            updateContactOpenScores(contacts);
        }
        if(!contactClickedIds.isEmpty()){
            List<Contact> contacts = getContacts(contactClickedIds);
            updateContactClickedScores(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 void updateContactClickedScores(List<Contact>contacts){
        System.debug('Number of contacts passed to update click score ' + contacts.size());

        List<Contact> contactsToUpdate = new List<Contact>();
        for (Contact c : contacts){
            c.Pardot_Emails_Opened__c +=1;
            c.Pardot_Links_Clicked__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;
    }
}
  • 2
    Hi Brooks, please take a moment to revisit the tour and How to Ask. Please note that if you post a Question, you should ask something. So "Any help appreciated" is not really the correct way to put it. You should instead ask "Why does..." or "How do I...". – Adrian Larson Feb 11 at 17:00
3

The relevant part of your code here is this:

if (c.Status == 'Opened' && oldMember.Status != 'Opened'){
    contactOpenIds.add(c.ContactId);
}if(c.Status == 'Clicked' && oldMember.Status != 'Clicked'){
    System.debug('Current member status = ' + c.Status);
    System.debug('Old Member status = ' + oldMember.Status);
    contactClickedIds.add(c.Id);
    System.debug('contactClickedIds.size() ' + contactClickedIds.size());
}

The main difference between those two if blocks is that in the "Opened" block, you're adding c.ContactId whereas in the "Clicked" block, you're adding c.Id (the Id of the CampaignMember)

CampaignMember is not a Contact, which is why your query on Contact is returning no rows.

This might be a lesson in why meaningful variable names are impoartant.

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.