Sunday 30 October 2011

Automatic Lead Coversion To Opportunity,Account and Contact based on Lead Status

I got new requirement in  Sales force  project.My Client ask new thing i.e  Automatic Lead Conversion To Opportunity based on Lead Status. i worked this task that time i Struggle but finally i finish this task.

here i am using trigger for Automatic Lead Conversion To Opportunity,Account and Contact  based on Lead Status.In Lead Status i  Created new value i.e "Set Appointment". User fills the Lead information that time choose Lead Status is "Set Appointment" after click on Save button then  Lead  converts Automatically  To Opportunity,Account and Contact.This code working newly  create Lead   and   Update  Lead also.



This is very useful code perfectly working.

Trigger :

............

trigger triggerConvertLead on Lead (after insert, after update)
{
  System.debug('Trigger Insert'+ Trigger.isInsert);
  System.debug('Trigger Update'+ Trigger.isUpdate);
  
 if(Trigger.isInsert && Trigger.new[0].Status == 'Set Appointment') {
   System.debug('Trigger Insert'+ Trigger.isInsert);
   System.debug('Trigger Update'+ Trigger.isUpdate);
  //System.debug('Trigger Upsert'+ Trigger.isUpdate);
 if((!Trigger.isInsert && (Trigger.new[0].Status == 'Set Appointment' && Trigger.old[0].Status != 'Converted' &&
     Trigger.new[0].Status !='Converted'))||
   (Trigger.isInsert && Trigger.new[0].Status == 'Set Appointment' && Trigger.new[0].Status != 'Converted')){      
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(Trigger.new[0].Id);
        LeadStatus convertstatus = [select Id, MasterLabel from LeadStatus where IsConverted= true limit 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvert[] lcArray = new Database.LeadConvert[] {lc};
        Database.LeadConvertResult[] results = Database.convertLead(lcArray);
        System.assert(results[0].IsSuccess());
     }
     else if(!Trigger.isUpdate && (Trigger.new[0].Status != 'Converted' ))
     {
   }
  }
 if(Trigger.isUpdate && Trigger.new[0].Status == 'Set Appointment')
  {
        //System.debug('Inside Trigger isUpdate ');
        //List<String> LeadNames = new List<String>{};
    for(Lead myLead: Trigger.new){
    if(myLead.IsConverted==false) {
    Database.LeadConvert lc = new database.LeadConvert();
         lc.setLeadId(myLead.Id);
         lc.convertedStatus = 'Set Appointment';
                //Database.ConvertLead(lc,true);
                //lc.setDoNotCreateOpportunity(true);
         Database.LeadConvertResult lcr = Database.convertLead(lc);
         System.assert(lcr.isSuccess());
           }
         }           //System.assert(results[0].IsSuccess());
      
      }
}

 i deployed this code from Sandbox to  Production for this purpose i write Test class to trigConvertLead.

Test Class:

 ....................

@isTest

  private class TestConvertLead {
    static testMethod void createLeadToConvert() {
      Lead newL = new Lead();
       newL.LastName = 'Test Lastname';
       newL.Company = 'Test Company';
       newL.Status = 'Set Appointment';
        insert newL;

    Lead newL2 = new Lead();
      newL2.LastName = 'Test Lastname';
      newL2.Company = 'Test Company';
      newL2.Status = 'Open';
        insert newL2;      
        //Lead UpdL = [Select Id from Lead where Id=: newL.Id];
    newL2.Status = 'Set Appointment';
        update newL2;

        Lead newL3 = new Lead();
    newL3.LastName = 'Test Lastname';
    newL3.Company = 'Test Company';
    newL3.Status = 'Open';
        insert newL3;      
        //Lead UpdL = [Select Id from Lead where Id=: newL.Id];
//    newL2.Status = 'Set Appointment';
//      update UpdL;

      
    //Lead Lead1 = [Select Id from Lead where Id=: newL.Id];
    List<Lead> listLead = [SELECT IsConverted, Status, Id FROM Lead WHERE Id =:newL3.Id limit 1];
  
    for(Lead l: listLead) {
      if(l.IsConverted==false) {
        Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(l.id);
            lc.convertedStatus = 'Set Appointment';
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
      }
    }

     }
}

1 comment:

  1. HI i see you have a brilliant work done here , i have a small doubt what would happen when there is an account already exists with this lead usually in the standard way it asks for do u want to merge with existing one what happens here?

    ReplyDelete