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();
           
    }
 
 
}



2 comments:

  1. Nowadays, most of the businesses rely on cloud based CRM tool to power their business process. They want to access the business from anywhere and anytime. In such scenarios, salesforce CRM will ensure massive advantage to the business owners. Salesforce Training | Salesforce Training in Chennai

    ReplyDelete
  2. Thanks for sharing informative article on Salesforce technology. Your article helped me a lot to understand the career prospects in cloud computing technology.
    cloud computing training centers in chennai|cloud computing training institutes in chennai

    ReplyDelete