+ Start a Discussion
sdfasdsdfasd 

How to Create Test class for Lead Conversion Trigger

i Create the trigger for lead conversion . now i need test codecoverage for Lead Conversion Trigger. how to create the test class for this trigger.pls help me................

 

Trigger

======

trigger ConvertLead on Lead (after insert, after update) {
    
    List<Id> accountIds = new List<Id>();
    List<Id> contactIds = new List<Id>();
    list<lead> leadobject = new List<lead>();
    
    List<RecordType> rtypes = [Select Name, Id From RecordType where sObjectType='Lead' and isActive=true];
    Map<String,String> leadRecordTypes = new Map<String,String>{};
    
    for(RecordType rt: rtypes){
        leadRecordTypes.put(rt.Name,rt.Id);
    }
    
    for(Lead led: Trigger.New){
        if(led.isConverted == true && led.Lead_type_picklist__c =='Broker' && led.RecordTypeId==leadRecordTypes.get('Broker')){
            if(led.ConvertedAccountId != null && led.ConvertedContactId != null )
               leadobject.add(led);
               accountIds.add(led.ConvertedAccountId);
               contactIds.add(led.ConvertedContactId);
        }
    }
    
    Map<Id,Account> accountMap = new Map<Id,Account>([Select Id,Name,BillingStreet,BillingCity,BillingState,County__c,BillingPostalCode,Phone from Account where Id IN: accountIds]);
    Map<Id,contact> contactMap = new Map<Id,contact>([Select Id,Name,Accountid from contact where Id IN: contactIds]);
    List<Agency__c > agencyinsert = new List<Agency__c >();
    List<Broker__c> brokers = new List<Broker__c>();
    
    for(integer i=0; i<leadobject.size(); i++){
        Id accId = leadobject[i].ConvertedAccountId;
        Id cId = leadobject[i].ConvertedContactId;
        Account accountobj = accountMap.get(accId);
        Contact contactobj = contactMap.get(cId);
        
        Agency__c ag = new Agency__c();
        ag.Name = accountobj.Name;
        ag.Mailing_Address__c = accountobj.BillingStreet;
        ag.City__c =accountobj.BillingCity;
        ag.State__c = accountobj.BillingState;
        ag.County__c = accountobj.County__c;
        ag.Zip_Code__c = accountobj.BillingPostalCode;  
        ag.Account__c = accountobj.Id;
        ag.Phone__c = accountobj.Phone;
        agencyinsert.add(ag);
    }
    if( agencyinsert.size()>0)
        insert agencyinsert;
 
  for(integer i=0; i<leadobject.size(); i++){
     for(integer j=0; j<agencyinsert.size(); j++){
         if(leadobject[i].ConvertedAccountId == agencyinsert[j].Account__c){
            Id cId = leadobject[i].ConvertedContactId;
            Contact contactobj = contactMap.get(cId);
            Broker__c b = new Broker__c();
            b.Name = contactobj.Name;
            b.Agency__c = agencyinsert[j].id;
            b.Contact_ID__c = contactobj.Id;
            brokers.add(b);
         }
     }
  }
 
}

 

pls help.................