You need to sign in to do that
Don't have an account?
timjock
Simple Unit test for a Trigger?
I'm new to Apex code and need to do a simple Unit test for this Trigger. How do I do this?
trigger associateLeadToCampaign on Lead (after insert) {
Set<String> campCode = new Set<String>();
//Capture the campaign code values for making using the query
for(Lead l : Trigger.new)
{
if(l.Campaign_Code__c != '')
campCode.add(l.Campaign_Code__c);
}
//Query all the Campaign records that match the campaign code
List<Campaign> campagn = [Select Id, Campaign_Code__c from Campaign where Campaign_Code__c IN :campcode];
Map<String, Campaign> campMap = new Map<String, Campaign>();
//Construct a map for later retrieval
for(Campaign c : campagn){
campMap.put(c.Campaign_Code__c, c);
}
List<CampaignMember> cmList = new List<CampaignMember>();
//Loop thru all the leads and create the Campaign Member records to associate lead to a matching campaign
for(Lead l : Trigger.new)
{
if(campMap.containsKey(l.Campaign_Code__c))
{
System.debug('The Lead ' + l.Id + ' can be associated to Campaign with Campaign_Code__c ' +l.Campaign_Code__c);
CampaignMember cm = new CampaignMember();
cm.CampaignId = campMap.get(l.Campaign_Code__c).Id;
cm.leadId = l.Id;
cm.Status = 'Sent';
cmList.add(cm);
}
}
insert cmList;
}
werewolf
Make your unit test insert a lead that meets the criteria in your if statements.