0

I have a trigger that adds a campaign if it is a "Sales Line" lead, to the lead. Unfortunately I cannot get more that 57% code coverage from my test class - I am at a loss on what to change. I am not sure what I need to do when the trigger adds the campaign based on there being no campaign, and matching the lead's leadsource. If you could just point me in the right direction - I have tried inserting the campaign and the campaignmember.

Trigger

trigger Create_Campaign_SalesLine on Lead (after insert) {

    try {  

        if (Trigger.new.size() == 1) {

            List <CampaignMember> cm = new list<CampaignMember>();

            for(Lead L : Trigger.new) {

                    String cname = L.leadsource;



                    List <Campaign> c = [select id, name from Campaign where name = :cname limit 1];

                    if(!c.isEmpty()){
                        CampaignMember cml = new CampaignMember();
                        cml.campaignid = c[0].id;
                        cml.leadid = l.id;
                        cm.add(cml);
                    }
            }

            if(!cm.isEmpty()){
                insert cm;
            }
        }


    } catch(Exception e) {
        system.debug ('error: ' + e.getMessage() );
        }
   }

Test Class

@isTest
private class TEST_createCampaignSalesLine {

    static testMethod void createCampaignTest() {


           Lead L1 = new Lead(
            lastname = 'Create_CampaignMember_For_New_Leads',
            firstname = 'Test For',
            company = 'Company Name',
            leadsource = 'Sales Line', 
            country = 'US'
            );

            insert L1;

        }
}
7
  • It would help to know which lines are covered and which are not. Typically that will reveal a conditional that is not being triggered. I would also recommend changing the trigger to be bulkified: rather than checking for a size of one, loop over the entire collection of new objects. Related question.
    – user6861
    Jun 3, 2014 at 16:51
  • Lines 18 - 21, 26 and 31. Typically sales line leads are added one at a time, and very infrequently. Would you still suggest bulkifying it? Jun 3, 2014 at 16:54
  • @Merry Always bulkify your code. You never know when it will cause problems later. That said, you also need to insert a campaign, because the campaign isn't visible in your test (nor should you use SeeAllData, because that is begging for future problems too).
    – sfdcfox
    Jun 3, 2014 at 16:57
  • @sfdcfox I will bulkify! I tried adding a campaign and nothing changed in the test coverage. Jun 3, 2014 at 17:00
  • @Merry did you insert the Campaign object before the Lead? The Campaign needs to be inserted first for the trigger to work correctly. I would also ask that you mark which lines are being skipped in the test, it is difficult to tell line numbers in the code block.
    – user6861
    Jun 3, 2014 at 17:07

1 Answer 1

3

First, Bulkify

trigger Create_Campaign_SalesLine on Lead (after insert) {
    Map<String, Id> campaigns = new Map<String, Id>();
    CampaignMember[] members = new CampaignMember[0];

    for(Lead record: Trigger.new) {
        campaigns.put(record.LeadSource, null);
    }

    for(Campaign record: [SELECT Id, Name FROM Campaign WHERE Name IN :campaigns.keyset()]) {
        campaigns.put(record.Name, record.Id);
    }

    for(Lead record: Trigger.new) {
        if(campaigns.get(record.LeadSource) != null) {
            members.add( 
                new CampaignMember(
                    CampaignId = campaigns.get(record.LeadSource), LeadId = record.Id, Status='Sent'));
        }
    }

    Database.SaveResult[] results = Database.insert(members, false);

    for(Integer index = 0; index < results.size(); index++) {
        if(!results[index].isSuccess()) {
            Trigger.newMap.get(members[index].LeadId).LeadSource.addError('Error adding lead to campaign: '+results[index].getErrors()[0].getMessage());
        }
    }
}

Test It

@isTest
private class TEST_createCampaignSalesLine {

    static testMethod void createCampaignTest() {
           Campaign c1 = new Campaign(Name='Sales Line', IsActive=true);
           insert c1;

           Lead L1 = new Lead(
            lastname = 'Create_CampaignMember_For_New_Leads',
            firstname = 'Test For',
            company = 'Company Name',
            leadsource = 'Sales Line', 
            country = 'US'
            );

            insert L1;

        }
}

A complete test will include the error handling, but this isn't necessary, or maybe even possible. Don't worry about 100% coverage; 80-90% is all you should strive for.

3
  • This line Database.CreateResult[] results = Database.insert(members, false); gives the error Invalid Type. Jun 3, 2014 at 17:54
  • @Merry I had a few typos. I didn't try it in my org beforehand. Updated.
    – sfdcfox
    Jun 3, 2014 at 18:21
  • Thanks for the updated code - and the code all together, 95% coverage. Jun 3, 2014 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .