1

We have a class that was previously deployed into production. I have made a small change to the class, simply modifying a string that contains the campaign name so we can use the class on a new campaign.

I have run the test to deploy the changed class however i am coming up againt the following errors

Debug

enter image description here

I have tried my best to figure out why this is i have made small changes to the class and testclass and so far nothing has worked, i have exhausted my knowledge and any help to point me in the right direction would be greatly appreciated

Here is the Test

 @isTest
private class CampaignMemberClassTest {

    private static UnitTestHelper helper = new UnitTestHelper();
    private static Campaign fakeCampaign;
    private static List<CampaignMemberStatus> fakeCampaignMemberStatuses;
    private static List<Contact> fakeContactList;
    private static List<CampaignMember> fakeCampaignMembers;


     static {
        // Create and Initialise the Contacts List
        fakeContactList = new List<Contact>();
        for (Integer i = 0; i < 10; i++) {
          fakeContactList.add(helper.createContact('Fake First Name ' + i, 'Fake Last Name ' + i, 'fake.email_' + i + '@email.com'));
        }
    }

    // Create a list of Statuses for a Campaign
    private static List<CampaignMemberStatus> createFakeCampaignMemberStatuses() {
        List<CampaignMemberStatus> fakeCampaignMemberStatusesList = new List<CampaignMemberStatus>();
        fakeCampaignMemberStatusesList.add(helper.createCampaignMemberStatus(fakeCampaign.Id, 'To Call', true, false, 3));
        fakeCampaignMemberStatusesList.add(helper.createCampaignMemberStatus(fakeCampaign.Id, 'Send an Email', true, false, 4));
        fakeCampaignMemberStatusesList.add(helper.createCampaignMemberStatus(fakeCampaign.Id, 'Not Interested', false, true, 5));
        fakeCampaignMemberStatusesList.add(helper.createCampaignMemberStatus(fakeCampaign.Id, 'Interested', false, true, 6));
        return (fakeCampaignMemberStatusesList);
    }

    // create a list of Contact members to a Campaign
    private static List<CampaignMember> createFakeCampaignMembersList() {
        List<CampaignMember> fakeCampaignMemberList = new List<CampaignMember>();
        for (Contact fakeContact : fakeContactList) {
            fakeCampaignMemberList.add(helper.createCampaignMember(fakeCampaign.Id, fakeContact.Id, 'To Call'));
        }
        return (fakeCampaignMemberList);
    }    

    private static void init(String campaignName) {
        fakeCampaign = helper.createCampaign(campaignName);
        insert(fakeCampaign);
        insert(fakeContactList);
        fakeCampaignMemberStatuses = createFakeCampaignMemberStatuses(); 
        insert(fakeCampaignMemberStatuses);
        fakeCampaignMembers = createFakeCampaignMembersList();
        insert(fakeCampaignMembers);
    }

    static testMethod void Does_CreateTask_When_MemberStatusIsInterested() {
        // ARRANGE
        init('2015 - PT - PowerMill con financiacion');
        Integer expectedResult = fakeCampaignMembers.size();
        System.debug('list is' + fakecontactlist.size() );

        // ACT
        Test.startTest();
        List<CampaignMember> fakeCampaignMembersToUpdate = new List<CampaignMember>();
        Integer i = 0;
        for (CampaignMember cm : fakeCampaignMembers) { 
            cm.Status = 'Interested';
            if (i == 0) {
                cm.Comments__c = 'Fake Comments';
            }
            fakeCampaignMembersToUpdate.add(cm);
              i = i == 0 ? 1 : 0;
        }
        update(fakeCampaignMembersToUpdate);
        Test.stopTest();

        // ASSERT
        Integer actualResult = [SELECT Id FROM Task WHERE WhoId IN :fakeContactList].size();
        System.assertequals(expectedResult, actualResult);
    }

    static testMethod void Does_NotCreateTask_When_MemberStatusIsNotInterested() {
        // ARRANGE
        init('2015 - PT - PowerMill con financiacion');
        Integer expectedResult = 0;

        // ACT
        Test.startTest();
        List<CampaignMember> fakeCampaignMembersToUpdate = new List<CampaignMember>();
        for (CampaignMember cm : fakeCampaignMembers) { 
            cm.Status = 'Not Interested';
            fakeCampaignMembersToUpdate.add(cm);
        }
        update(fakeCampaignMembersToUpdate);
        Test.stopTest();

        // ASSERT
        Integer actualResult = [SELECT Id FROM Task WHERE WhoId IN :fakeContactList].size();
        System.assertequals(expectedResult, actualResult);
    }

    static testMethod void Does_NotCreateTask_When_CampaignNameIsDifferent() {
        // ARRANGE
        init('2014 - UK - FIBO');
        Integer expectedResult = 0;

        // ACT
        Test.startTest();
        List<CampaignMember> fakeCampaignMembersToUpdate = new List<CampaignMember>();
        for (CampaignMember cm : fakeCampaignMembers) { 
            cm.Status = 'Interested';
            fakeCampaignMembersToUpdate.add(cm);
        }
        update(fakeCampaignMembersToUpdate);
        Test.stopTest();

        // ASSERT
        Integer actualResult = [SELECT Id FROM Task WHERE WhoId IN :fakeContactList].size();
        System.assertequals(expectedResult, actualResult);
    }

    static testMethod void Does_CreateEmail_When_MemberStatusIsSendAnEmail() {
        // ARRANGE
        init('2015 - PT - PowerMill con financiacion');
        Integer expectedResult = 10;

        // ACT
        Test.startTest();
        List<CampaignMember> fakeCampaignMembersToUpdate = new List<CampaignMember>();
        for (Integer i = 0; i < expectedResult; i++) {
            fakeCampaignMembers.get(i).Status = 'Send an Email';
            fakeCampaignMembersToUpdate.add(fakeCampaignMembers.get(i));
        }
        update(fakeCampaignMembersToUpdate);
        Test.stopTest();

        // ASSERT
        Integer actualResult = [SELECT Id FROM Task WHERE WhoId IN :fakeContactList].size();
        System.assertequals(expectedResult, actualResult);
    }

}

And the Class

public with sharing class CampaignMemberClass {

    public static void processCampaignMembers(List<CampaignMember> campaignMembersNew, Map<Id, CampaignMember> campaignMembersOld) {

        Id campaignId;
        Id companyIberiaEmailId = [SELECT Id FROM OrgWideEmailAddress WHERE Address = 'info.iberia@comapny.com' LIMIT 1].Id;

        // Structure of campaignMembersToCreateTaskMap. KEY: Contact Id. VALUE: CampaignMember Id
        Map<Id, Id> campaignMembersToCreateTaskMap = new Map<Id,Id>();
        // Structure of campaignMembersToEmailMap. KEY: Contact Id. VALUE: CampaignMember Id
        Set<Id> campaignMembersToCreateEmailSet = new Set<Id>();        
        try {
            campaignId = [SELECT Id FROM Campaign WHERE Name = '2015 - PT - PowerMill con financiacion' LIMIT 1].Id;

            for (CampaignMember cm : campaignMembersNew) {

                if (cm.CampaignId == campaignId) {
                    // Task needs to be created for the Owner of the Contact if the Contact is interested
                    if (cm.Status == 'Interested' && campaignMembersOld.get(cm.Id).Status != 'Interested') {
                        campaignMembersToCreateTaskMap.put(cm.ContactId, cm.Id);
                    }
                    // Email needs to be sent to the Contact
                    else if (cm.Status == 'Send an Email' && campaignMembersOld.get(cm.Id).Status != 'Send an Email') {
                        campaignMembersToCreateEmailSet.add(cm.ContactId);
                    }
                }
            }
        } catch (Exception e) {
            System.debug('The Campaign 2015 - PT - PowerMill con financiacion does not exists');
        }

        if (!campaignMembersToCreateTaskMap.isEmpty()) {
            createCampaignMemberTask(campaignMembersToCreateTaskMap, companyIberiaEmailId);
        }
        if (!campaignMembersToCreateEmailSet.isEmpty()) {
            sendEmailToCampaignMember(campaignId, campaignMembersToCreateEmailSet, companyIberiaEmailId);
        }
    }

    private static void createCampaignMemberTask(Map<Id, Id> contactsToCreateTaskMap, Id senderEmailId) {

        Map<Id, Contact> contactOwnerIdsMap = new Map<Id, Contact>([SELECT Id, OwnerId FROM Contact WHERE Id IN :contactsToCreateTaskMap.keySet()]);
        Map<Id, CampaignMember> campaignMemberMap = new Map<Id, CampaignMember>([SELECT Id, CampaignId, Comments__c FROM CampaignMember WHERE Id IN :contactsToCreateTaskMap.values()]);
        List<Task> tasksToCreateList = new List<Task>();

        for (Id contactId : contactOwnerIdsMap.keySet()) {
            Task newTask = new Task();
            newTask.OwnerId = contactOwnerIdsMap.get(contactId).OwnerId;
            newTask.Subject = 'Concretar Visita';
            newTask.Type__c = 'Phone Call Outbound';
            newTask.Sub_Type__c = 'Follow Up - Sales Visit';
            newTask.ActivityDate = Date.Today() + 2;
            newTask.Campaigns__c = '2015 - PT - PowerMill con financiacion';
            newTask.Originated_by_Marketing__c = true;
            newTask.Status = 'Not Started';
            newTask.Priority = 'Normal';
            newTask.WhatId = campaignMemberMap.get(contactsToCreateTaskMap.get(contactId)).CampaignId;
            newTask.WhoId = contactId;
            if (campaignMemberMap.get(contactsToCreateTaskMap.get(contactId)).Comments__c != null) {
                newTask.Description = campaignMemberMap.get(contactsToCreateTaskMap.get(contactId)).Comments__c.stripHTMLTags();
            }
            tasksToCreateList.add(newTask);
        }

        if (!tasksToCreateList.isEmpty()) {
            insert(tasksToCreateList);
            sendEmailToTaskAssignee(tasksToCreateList, senderEmailId);
        }
    }

    private static void sendEmailToCampaignMember(Id campaignId, Set<Id> contactsToEmailSet, Id senderEmailId) {
        Id emailTemplateId = [SELECT Id FROM EmailTemplate WHERE Name = '2015 - PT - PowerMill con financiacion Email' LIMIT 1].Id;

        for (Contact c : [SELECT Id, Email FROM Contact WHERE Id IN :contactsToEmailSet AND Email != NULL AND HasOptedOutOfEmail = false]) {
            Messaging.SingleEmailMessage emailToContact = new Messaging.SingleEmailMessage();
            emailToContact.setOrgWideEmailAddressId(senderEmailId);
            emailToContact.setTemplateId(emailTemplateId);
            emailToContact.setTargetObjectId(c.Id);
            emailToContact.setWhatId(campaignId);
            emailToContact.setSaveAsActivity(true);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emailToContact });
        }
    }

    private static void sendEmailToTaskAssignee(List<Task> tasksToEmailList, Id senderEmailId) {

        String emailMessage = 'Tienes una tarea pendiente de la campaña 2015 - PT - PowerMill con financiacion. El link para la tarea es:<br/>';
        Set<Id> userIds = new Set<Id>();
        for (Task currentTask : tasksToEmailList) {
            userIds.add(currentTask.OwnerId);
        }
        Map<Id, User> userEmails = new Map<Id, User>([SELECT Id, Email FROM User WHERE Id IN :userIds]);

        for (Task currentTask : tasksToEmailList) {
            emailMessage += '<a href=\'https://login.salesforce.com/' + currentTask.Id + '\'>https://login.salesforce.com/' + currentTask.Id + '</a>';
            Messaging.SingleEmailMessage emailToAssignee = new Messaging.SingleEmailMessage();
            emailToAssignee.setOrgWideEmailAddressId(senderEmailId);
            emailToAssignee.setToAddresses(new String[]{userEmails.get(currentTask.OwnerId).Email});
            emailToAssignee.setSubject('Tarea de 2014 - ES - Trade-Out FlexStrider');
            emailToAssignee.setHTMLBody(emailMessage);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { emailToAssignee });            
        }
    }
}

Calling Trigger

trigger CampaignMemberTrigger on CampaignMember (after update) {

    if (Trigger.isAfter) {
        if (Trigger.isUpdate) {
          CampaignMemberClass.processCampaignMembers(Trigger.new, Trigger.oldMap);
        }
    }
}
  • Please post the changes you made, the failure messages, the debug messages, and the updated code with the campaign name strings fixed – Rob Sep 24 '15 at 16:06
  • Hi The changes i made no longer exist as they didnt not help me and some made it worse, this the code that was originally deployed to production just with the campaign name changed. I have posted the debug messages and the code with the strings amended. – user24879 Sep 24 '15 at 16:15
  • Are the static methods of CampaignMemberClass being called from a trigger? And did you check that the trigger is firing? – Developer Wonk Sep 24 '15 at 20:08
  • Add system.debug('FakeCampaignMemberList Size: ' + fakeCampaignMembersList.size()) before exiting the method createFakeCampaignMembersList(). In Does_CreateTask_When_MemberStatusIsInterested() add system.debug('expected result is: ' expectedResult)). Also add a system.debug() to display the actualResults in this test method. Based on the error message one of these is not the correct result. Have you verified that actualResults is not 10. Could it be that expectedResults is not 10? – Developer Wonk Sep 25 '15 at 12:55
  • @user24879 - What is the code to createFakeCampaign (or debug the value of the campaign created). Maybe it's values do not match what the trigger is looking for? – Eric Sep 25 '15 at 12:59
0

In your test class you're inserting a campaign with the name:

'2015 - PT - PowerMill con financiacion '

Then in CampaignMemberClass you're attempting to attempting to retrieve:

'2015 - PT - PowerMill con financiacion'

Note the extra space at the end of the string in your test class.

| improve this answer | |
  • Hi thanks for pointing that out however it has made no difference to the failures – user24879 Sep 24 '15 at 15:58
0

The trigger that calls on CampaignMemberClass has either been disabled or deleted in your production org.

| improve this answer | |
  • Hi i thought of that however the trigger is active, i have included it in the question. thank you – user24879 Sep 25 '15 at 8:12

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.