You need to sign in to do that
Don't have an account?
Joe 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;
}
}
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;
}
}
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/page/How_to_Write_Good_Unit_Tests
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
Please use the below code for your test class,