+ Start a Discussion
sachitanand kumarsachitanand kumar 

How to write test class for this lead trigger

Hello everyone, plz tell me how to write the test class for this trigger listed below.

trigger trigMapFields on Lead (before update) {
   Map<Id,String> leadStatus = new Map<Id,String>();
   for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            leadStatus.put(lead.ConvertedContactId,lead.channel__c);
        }
    }
    List<Contact> conContacts = [select Id from Contact WHERE Contact.Id IN :leadStatus.keySet()];
    for ( Contact c : conContacts) {
        c.channel__c= leadStatus.get(c.Id);
    }
    update conContacts;
}
karthikeyan perumalkarthikeyan perumal
Hello

use below code 
 
@isTest
public class LeadBeforeupdate_Test{
 
      
     public Static testMethod void LeadBeforeupdate(){
     
       Test.StartTest(); 
       
       Contact TestCon= new Contact();
	   TestCon.FirstName='TestFIrst';
	   TestCon.LastName='TestLastname';
	   insert TestCon;
	   
	   Lead TestLead Lead();
	   TestLead.name='testLead';
	   TestLead.IsConverted=true;
	   TestLead.ConvertedContactId=TestCon.Id;
	   TestLead.channel__c='Testchanel';
	   
	   insert TestLead;
	   
	   update TestLead;		
       
       Test.StopTest();
      
       
      }
      
}

Hope this will help you, 

Thanks
karthik