Ticker

6/recent/ticker-posts

CUSTOM 'LEAD__C' CONVERSION PROCESS USING TRIGGER





     

                                     B2B                                                                                       B2C


CUSTOM 'Lead__c' (B2B/B2C) CONVERT INTO 'ACCOUNT + CONTACT' / 'PERSON ACCOUNT'
  • Custom Lead__c convert by using 'Trigger'.
  • I've using Trigger, Batch Class, Schedule Class.
  • After conversion Lead__c record, automatically delete Lead__c within 20 seconds.  
  • B2B Lead__c after conversion to create Business Account & Account-Related Contact Record.
  • B2C Lead__c after conversion to create Person Account Record.



Trigger -: LeadConvert
trigger LeadConvert on Leads__c (after update) {
    List<ID> ConvertedLeadID = New List<ID>();
    list<string> hh = new list<string>();
    list<account> app =new list<account>();
    list<contact> caa = new list<contact>();
    Id AccRecordTypeId = Schema.SObjectType.Leads__c.getRecordTypeInfosByName().get('Provider').getRecordTypeId();
    Id Acc1RecordTypeId = Schema.SObjectType.Leads__c.getRecordTypeInfosByName().get('Patient').getRecordTypeId();
    RecordType rt1 = [select id,Name from RecordType where SobjectType='Account' AND recordtype.name='Provider'];
    RecordType rt2 = [select id,Name from RecordType where SobjectType='Account' AND recordtype.name='Patient'];
    Boolean process=false;
    for(Leads__c bb :trigger.new){
        if( bb.Lead_Status__c =='Converted' && trigger.oldMap.get(bb.id).Lead_Status__c !='Converted'){
            process=true;
            ConvertedLeadID.add(bb.Id);
            if(bb.Hospital_Name__c!=null && bb.Hospital_Name__c!='' )
                hh.add(bb.Hospital_Name__c);
        }
        
    }
    if(process){
        
        
        Map<string,id> mapAcc = new Map<string,id>();
        //mapAcc.keySet();
        
        for(account acc :[select id,name from account where name in:hh]){
            
            mapAcc.put(acc.name.toLowerCase(),acc.id);
            system.debug('map'+mapAcc);
        }
        Map<string,Account> mapAccToInsert = new Map<string,Account>();
        for(Leads__c ac : trigger.new){
            if(ac.Lead_Status__c =='Converted' && trigger.oldMap.get(ac.id).Lead_Status__c !='Converted'){
                if(String.isNotBlank(ac.Hospital_Name__c) && mapAcc!=Null && !mapAcc.containsKey(ac.Hospital_Name__c.toLowerCase()) && ac.RecordTypeId == AccRecordTypeId){
                    account ap = new account();
                    ap.name=ac.Hospital_Name__c;
                    ap.Website=ac.Website__c;//b2b rectype
                    mapAccToInsert.put(ac.Hospital_Name__c.toLowerCase(),ap);
                }
                else if(ac.RecordTypeId == Acc1RecordTypeId){
                    account kk = new account();
                    kk.RecordTypeId=rt2.ID;//b2c recordtype
                    kk.Relationship__c=ac.Relationship__c;
                    kk.If_Other_Please_Specify__c = ac.If_Other_Please_Specify__c;
                    kk.Guardian_First_Name__c=ac.Guardian_First_Name__c;
                    kk.Guardian_Middle_Name__c=ac.Guardian_Middle_Name__c;
                    kk.Guardian_Last_Name__c=ac.guardian_Last_Name__c;
                    kk.race__c=ac.race__c;
                    kk.Phone=ac.Phone_1__c;
                    kk.Email_Address_Work__c=ac.Email_1__c;
                    kk.FirstName=ac.First_Name__c;
                    kk.MiddleName=ac.Middle_Name__c;
                    kk.LastName=ac.Last_Name__c;
                    kk.Gender__pc=ac.Gender__c;
                    kk.Date_of_Birth__c=ac.Date_of_Birth__c;
                    kk.Social_Security_Number__c=ac.Social_Security_Number__c;
					kk.BillingCity=ac.City__c;
                    kk.BillingStreet=ac.Street_Address__c;
                    kk.BillingState=ac.State__c;
                    kk.BillingCountry=ac.Country__c;
                    kk.BillingPostalCode=string.valueOf(ac.Zip_Postal_Code__c);
                    app.add(kk);
                }
            }}
        insert app;
        insert mapAccToInsert.values();
        for(Leads__c ac : trigger.new){
            if(ac.Lead_Status__c =='Converted' && trigger.oldMap.get(ac.id).Lead_Status__c !='Converted' && ac.RecordTypeId == AccRecordTypeId){
                if(mapAcc.containsKey(ac.Hospital_Name__c.toLowerCase())){
                    contact con = new contact();
                    con.FirstName=ac.First_Name__c;
                    con.MiddleName=ac.Middle_Name__c;
                    con.lastname=ac.last_name__c;
                    con.Religion_Belief__c=ac.Religion_Belief__c;
                    con.Race__c=ac.Race__c;
                    con.Phone_1_Type__c=ac.Phone_1_Type__c;
                    con.Phone=ac.Phone_1__c;
                    con.Phone_2__c=ac.Phone_2__c;
                    con.Phone_2_Type__c=ac.Phone_2_Type__c;
                    con.Email=ac.Email_1__c;
                    con.Email_2__c=ac.Email_2__c;
                    con.Marital_Status__c=ac.Marital_Status__c;
                    con.Date_of_Birth__c=ac.Date_of_Birth__c;
                    con.Description=ac.Description__c;
                    con.Title=ac.Title__c;
                    con.MailingCity=ac.City__c;
                    con.MailingStreet=ac.Street_Address__c;
                    con.MailingState=ac.State__c;
                    con.MailingCountry=ac.Country__c;
                    con.MailingPostalCode=String.valueOf(ac.Zip_Postal_Code__c);
                    con.accountid=mapacc.get(ac.Hospital_Name__c.toLowerCase());
                    system.debug('===='+mapacc.get(ac.Hospital_Name__c.toLowerCase()));
                    caa.add(con);
                }
                else{
                    contact ca = new  contact();
                    ca.AccountId=mapAccToInsert.get(ac.Hospital_Name__c.toLowerCase()).Id;
                    ca.FirstName=ac.First_Name__c;
                    ca.MiddleName=ac.Middle_Name__c;
                    ca.LastName=ac.Last_Name__c;
                    ca.Race__c=ac.Race__c;
                    ca.Religion_Belief__c=ac.Religion_Belief__c;
                    ca.Phone_1_Type__c=ac.Phone_1_Type__c;
                    ca.Phone=ac.Phone_1__c;
                    ca.Phone_2__c=ac.Phone_2__c;
                    ca.Phone_2_Type__c=ac.Phone_2_Type__c;
                    ca.Email=ac.Email_1__c;
                    ca.Email_2__c=ac.Email_2__c;
                    ca.Marital_Status__c=ac.Marital_Status__c;
                    ca.Date_of_Birth__c=ac.Date_of_Birth__c;
                    ca.Description=ac.Description__c;
                    ca.Title=ac.Title__c;
                    ca.MailingCity=ac.City__c;
                    ca.MailingStreet=ac.Street_Address__c;
                    ca.MailingState=ac.State__c;
                    ca.MailingCountry=ac.Country__c;
                    ca.MailingPostalCode=String.valueOf(ac.Zip_Postal_Code__c);
                    caa.add(ca);
                }
            }
        }
        insert caa;
        LeadBatchScheduled bt = New LeadBatchScheduled(ConvertedLeadID);
        //bt.convertedLeadID=ConvertedLeadID;
        //database.executeBatch( bt);
        DateTime dtt = system.now().addseconds(20);
        String sch4 = dtt.Second() +' '+dtt.Minute() +' '+ dtt.Hour() +' '+dtt.Day()+' '+dtt.Month()+' ? '+dtt.Year();
        System.schedule('Schedule Job4'+sch4 , sch4, bt);
                
        
    }
    
}


Batch Class -: LeadBatch 
Global class LeadBatch implements database.Batchable <sObject>
{
    Global list<ID> convertedLeadID = New List<ID>();
	Global database.QueryLocator start(database.BatchableContext dc)
    { 
       return database.getQueryLocator('select id from Leads__c where ID in : convertedLeadID'); 
    }
    Global void execute(database.BatchableContext b,list<sObject> obj)
    {
        delete obj;    
    }
    Global void finish(database.BatchableContext b)
    {
        //optional
    }
}


Scheduled Class -: LeadBatchScheduled
global class LeadBatchScheduled implements schedulable
{
    list<ID> LD = New List<ID>();
    Global LeadBatchScheduled(){
       
    }
    Global LeadBatchScheduled(List<ID> leadIDs){
        this.LD=leadIds;
    }
	global void execute(schedulableContext ctx)
    {
        LeadBatch kl = new LeadBatch();
        kl.convertedLeadID=this.LD;
        database.executeBatch(kl);
    }
}

Post a Comment

0 Comments