Wednesday, 18 January 2012

Test Class on trigger involved lead conversion


//When the lead is converted to account and contact then the cases under that lead carried to account and contact created from lead conversion

trigger leadtrig on Lead (after update) {
        list<Id> leadids = new list<Id>();
        for(Lead objLead:trigger.new){
                if(objLead.IsConverted)
                    leadids.add(objLead.id);
        }
        list<case> lstcase=[Select id from case where Lead__c in:leadids];
        List<Case> lstcasetoupdate = new List<case>();
        if(lstcase != null){
        for(Lead obj: trigger.new){
            if(obj.IsConverted){
                    for(case c: lstcase){
                    c.accountid = obj.convertedAccountid;
                    c.contactid = obj.convertedContactid;
                    c.Lead__c = null;
                    lstcasetoUpdate.add(c);
            }
          }
        }
        if(lstcasetoUpdate.size()>0){
                update lstcasetoUpdate;
                }
        }
     
}

================================Test class on Trigger involves lead conversion as above=================


@isTest
private class Testonleadtrig {
     private static Id leadId;
       static {
       
    Lead testLead = new Lead();
        testLead.FirstName = 'Test First';
        testLead.LastName = 'Test Last';
        testLead.Company = 'Test Co';
        testLead.npe01__Preferred_Phone__c='Home';
        testLead.Status='Untouched';
        testLead.Company='Appshark';
       
        insert testLead;
       
        leadId = testLead.Id;
   
      }
   
    static testMethod void Test_LeadConvert(){
    Test.startTest();
        Case csobj=new Case();
            csobj.Subject='Email';
            csobj.Status='New';
            csobj.Origin='Email';
            csobj.Lead__c=LeadId;
            insert csobj;
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(LeadId);
       
        LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
       
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
    Test.stopTest();
           
    }
 
 
}



No comments:

Post a Comment