0

I have created a trigger on campaign member. Trigger gets 66% code coverage in sandbox and 0% in production. I know there's a million threads on this but none have solved my dilemma. Here is my trigger code:

trigger campaignMemberTrigger on CampaignMember (after insert, before update){

  private static boolean firstRun = true;
  cmpnMemberTriggerHandler handler = new cmpnMemberTriggerHandler();

  try{
    if(firstRun){
        firstRun = false;
        handler.execute();
    }
  }catch(exception e){
        system.debug('Error during campaignMemberTrigger: '+e.getMessage());
        for(campaignMember mem :(List<CampaignMember>)Trigger.new){

        mem.addError('Unable to process transaction, please try again or contact your administrator if you need assistance ');
    }
  }
}

Here is my test code:

@isTest
public class campaignMemberTriggerTests {

  public static testMethod void unsyncWithMarketoTest(){
    //create test data
    Campaign cm = new campaign(name = 'Pardot - Q317 - Marketplace Outbound Drip Campaign');
    insert cm;
    Lead ld = new Lead(status = 'Open', leadSource = 'Marketplace Upsell', lastName = 'Nila', state = 'IN', numberOfEmployees = 300, company = 'wois', email = 'adamnila@hobolala.com');
    insert ld;
    //that we have our test lead and campaign, we will create a new campaign member and see if the lead updates
    CampaignMember cmm = new campaignMember(campaignId = cm.id, leadid = ld.id);
    //time for our test
    test.startTest();
    insert cmm;
    test.stopTest();
    //verify that our code ran
    system.assertEquals(1, [SELECT count() FROM Lead WHERE dont_sync_with_marketo__c = true]);
    system.assertEquals(1, [SELECT count() FROM CampaignMember]);
  }

  public static testMethod void unsyncWithMarketoNegTest(){
    List<Lead> lds = new List<Lead>();
    Lead ld = new Lead(company = 'Failure');
    lds.add(ld);
    cmpMemDontSyncWithMarketo dsm = new cmpMemDontSyncWithMarketo();
    dsm.unsyncFromMarketo(lds);
  }
}

I cannot run all tests in production currently(long story, I just got here and it's a mess inside) so I need this class to succeed but I can't see any reason inserting a campaign member wouldn't run the trigger?

John Thompson
  • 2,109
  • 1
  • 14
  • 26
SFDC Noob
  • 378
  • 4
  • 12
  • Any errors at all? do you make it to the line where the CM is inserted, if so the coverage not he trigger would not be 0% as at least the first line would be covered. Adding liberal asserts for Id values, and using database method to do DML with all or none false so you can see errors would be helpful when reviewing the logs or throwing the error as a custom error with the DML fail message – Eric Sep 27 '17 at 03:19

2 Answers2

2

There's probably some kind of validation rule on campaign or lead that's preventing your records from being inserted (possibly campaignMember too). Check to see what fields are required in the production org in order to insert a record. That's the most likely reason you're not getting the coverage.

Looking at your debug logs should tell you what's causing the lack of coverage. You may find it helpful to add debug statements to your code as you troubleshoot the issue. See How do I start to debug my own Apex code?.

crmprogdev
  • 40,955
  • 9
  • 58
  • 115
0

This sort of situation is exactly why I have gotten into the habit of always creating my test data in one @testSetup decorated method, and validating that it was actually created as expected with a corresponding testMethod whose only job is to verify that the test data was setup correctly. Here's a link to my unit test Gist that's tailored as an Eclipse template. Here's what's inside...

@isTest
private class myTestClass {
//setup testClass variables here. For example:
//RecordType Ids
/*private static final Id Customer_RECORD_TYPE_ID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Customer').getRecordTypeId();*/
//A common Loop Counter for bulikification purposes
/*private static final integer MAX_LOOP_COUNTER = 200;*/

  @testSetup static void setupTestData(){
    //setup common test data here, and insert records as needed....
  }

  static testMethod void testSetupData(){
    //use this method to validate that you have test data setup correctly
    //this will protect you from changes to workflow, process, and validation that break your test code!
    //Frequently a SOQL query and a count of expected records will suffice.
  }

  static testMethod void myUnitTest() {
    // TO DO: implement unit test AFTER you have your test data all setup and verified as OK
  }
}

It's a little bit of overhead, BUT it's really paid off when someone creates a new validation rule and doesn't tell me!

If you apply this unit test pattern, you will at least know for sure that your test data is actually getting setup correctly in any Org that you run it in. You are going to want to have that for when you test your before update context.

John Thompson
  • 2,109
  • 1
  • 14
  • 26