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