Convert Lead to Account and Auto Create an Opportunity Upon Booking Meeting

Note: this process uses triggers

Upon creating an event for a lead via Chili Piper Calendar Round Robin, you may use Salesforce triggers and a bit of APEX code to change the lead owner to the event’s assignee, convert the lead to a contact/account and open an opportunity, all automatically.

Here are the steps:

Assign and Convert the Lead

When a new event related to a lead is created set a trigger per below. The trigger will:

  • set the lead owner = assignee
  • call a class for conversion

Class for conversion.

Navigate to Salesforce Setup page -> App Setup -> Develop -> Apex Classes

apex-class

Add the following code to a new class:

global class AutoConvertLeads implements Schedulable{

global Set<Id> LeadIds;

global void execute(SchedulableContext sc) {

LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
for(Id leadId : LeadIds){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(leadId);
Leadconvert.setConvertedStatus(Leads.MasterLabel);
Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
System.assert(Leadconverts.isSuccess());
System.debug(‘LeadAssign Success’);
}
System.abortJob(sc.getTriggerId());
}

public AutoConvertLeads (Set<Id> LeadIds) {
this.LeadIds=LeadIds;
}

}

Opportunity creation

This class above creates an opportunity with the account name as the default name of the opportunity & default stage.

Add Trigger

Add the trigger that will call the above class with a delay of 10 seconds:

Navigate to App Setup ->Customize -> Activities -> Event Triggers

apex-trigger

Add the following code. You may change “Demo Small Business’” and “Demo Mid Market”  with the meeting types you already setup in Chili Piper.

trigger ChangeTheOwnerAndConvert on Event (after insert) {

   Set<Id> LeadIds = new Set<Id>();

   for (Event e: Trigger.new){

   if (e.WhoId != null){

        String eventNameId = e.WhoId;

        if (eventNameId.startsWith(’00Q’) && !String.isBlank(e.Meeting_Type_CP__c)){

            if (e.Meeting_Type_CP__c.equals(‘Demo Small Business’) ||

                                             e.Meeting_Type_CP__c.equals(‘Demo Mid Market’)){

                 LeadIds.add(e.WhoId);

                 Lead l = new Lead( Id = e.WhoId);

                 l.OwnerId = e.OwnerId;

                 update l;

                }

          }     

     }

   }

   if(LeadIds.size() > 0){

       Datetime sysTime = System.now();

       sysTime = sysTime.addSeconds(10);

       String chron_exp = ” + sysTime.second() + ‘ ‘ + sysTime.minute() + ‘ ‘ + sysTime.hour() + ‘ ‘ + sysTime.day() + ‘ ‘ + sysTime.month() + ‘ ? ‘ + sysTime.year();

       System.schedule(‘ConvertLead’ + UserInfo.getUserId() +sysTime,chron_exp,new AutoConvertLeads(LeadIds));

   }

}

Do not forget to activate the trigger at the end:

apex-activate

Still need help? Contact Us Contact Us