+ Start a Discussion
Joe DiasJoe Dias 

Need test class for my first trigger

I have this trigger working fine in sandbox,however to push it into production I need to write a test class,I am a newbie and never written a test class prior. Can some one help me with this. Below is the trigger code. Thanks.trigger CampaignMember on CampaignMember (after insert, after update) {

    List<CampaignMember> memberList=[SELECT LeadId,CampaignId FROM CampaignMember WHERE ID IN:Trigger.new];


    Set<ID> leadIDs=new Set<ID>();
    Set<ID> campaignIDs=new Set<ID>();

    for(CampaignMember member:memberList)
    {
        leadIDs.add(member.LeadId);
        campaignIDs.add(member.CampaignId);
    }

    Map<Id,Lead> leadMap=new Map<Id,Lead>([SELECT Id,Recent_Campaign__c FROM Lead WHERE Id IN:leadIDs]);

    Map<Id,Campaign> campaignMap=new Map<Id,Campaign>([SELECT Id,Name FROM Campaign WHERE Id IN:campaignIDs]);

    if(Trigger.isAfter && Trigger.isInsert)
    {
        List<Lead> leadsToUpdate=new List<Lead>();    

        for(CampaignMember member:memberList)
        {
            Campaign campaign=campaignMap.get(member.CampaignId);
            Lead lead=leadMap.get(member.LeadId);
            lead.Recent_Campaign__c=campaign.Name;
            leadsToUpdate.add(lead);
        }

        update leadsToUpdate;           
    }

    if(Trigger.isAfter && Trigger.isUpdate)
    {
        List<Lead> leadsToUpdate=new List<Lead>();    

        for(CampaignMember member:memberList)
        {
            Campaign campaign=campaignMap.get(member.CampaignId);
            Lead lead=leadMap.get(member.LeadId);
            lead.Street=campaign.Name;
            leadsToUpdate.add(lead);
        }

        update leadsToUpdate;           
    }
}
Seb OrtizSeb Ortiz
Hey, have a look to these very nice tutorials. You'll build it up easily

https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests

Cloud_forceCloud_force
simply creating new compaign member record and then updating the same record would give you most of the coverage for this trigger
as an example,
create test lead,compaign and contact records say campRec,conRec,LeadRec. and then create CampaignMember record using these three record id's

CampaignMember b = new CampaignMember();
b.CampaignId=camRec.id;
b.ContactId=conRec.id;
b.LeadId=leadRec.id;
       // Insert record
       insert b;
b.fieldmname = 'test';update b;

thanks,
http://www.forcexplore.com/2014/01/salesforce-interview-question-1.html

Arunkumar RArunkumar R
Hi joe,

Please use the below code for your test class,

@isTest
private class CampaignMemberTest
{

@isTest static void createCampaign()
{
Campaign c = new Campaign();
c.Name = 'Test Campaign';
insert c;

Contact cnt = new Contact();
cnt.LastName = 'Test Contact';
insert cnt;

Lead l = new Lead();
l.LastName = 'Test Lead';
l.Company= 'Test company';
insert l;

CampaignMember cm = new CampaignMember();
cm.CampaignId = c.Id;
cm.ContactId = cnt.Id;
cm.leadId = l.Id;
insert cm;

update cm;


}

}


Joe DiasJoe Dias
Thank you all for the help!!