+ Start a Discussion
Krishan Patel 1Krishan Patel 1 

Apex Trigger Not Firing

Hey everyone! I'm still very new to Apex Code, but I've been trying to implement Chili Piper, and a portion of it allows you to automatically convert leads to contacts and opportunities: https://support.chilipiper.com/article/70-convert-lead-to-account-and-auto-create-an-opportunity-upon-booking-meeting

I've created a Sandbox environment, and built this Apex 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;
}

}

Then I created this event trigger:
 
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 (!String.isBlank(e.Meeting_Type_CP__c)){
            if (e.Meeting_Type_CP__c.equals('Demo Request - Standard')){
                 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));
   }
}

The Event Trigger is checked as "Is Active", but when I create a lead and create an event on that least with the Meeting Type = Demo Request - Standard, nothing happens.

Does anyone have insight into this? Would love any help!
Best Answer chosen by Krishan Patel 1
Raj VakatiRaj Vakati
try this
 
trigger ChangeTheOwnerAndConvert on Event (after insert) {
   Set<Id> LeadIds = new Set<Id>();
   List<Lead> les = new List<Lead>() ;
   for (Event e: Trigger.new){
   if (e.WhoId != null){
        String eventNameId = e.WhoId;
        if (!String.isBlank(e.Meeting_Type_CP__c)){
            if (e.Meeting_Type_CP__c=='Demo Request - Standard'){
                 LeadIds.add(e.WhoId);
                 Lead l = new Lead( Id = e.WhoId);
                 l.OwnerId = e.OwnerId;
                 //update l;
				 les.add(l);
                }
          }     
     }
   }
   update les ; 
   
   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));
   }
}

 

All Answers

Raj VakatiRaj Vakati
try this
 
trigger ChangeTheOwnerAndConvert on Event (after insert) {
   Set<Id> LeadIds = new Set<Id>();
   List<Lead> les = new List<Lead>() ;
   for (Event e: Trigger.new){
   if (e.WhoId != null){
        String eventNameId = e.WhoId;
        if (!String.isBlank(e.Meeting_Type_CP__c)){
            if (e.Meeting_Type_CP__c=='Demo Request - Standard'){
                 LeadIds.add(e.WhoId);
                 Lead l = new Lead( Id = e.WhoId);
                 l.OwnerId = e.OwnerId;
                 //update l;
				 les.add(l);
                }
          }     
     }
   }
   update les ; 
   
   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));
   }
}

 
This was selected as the best answer
Krishan Patel 1Krishan Patel 1
@Raj Vakati

That worked! Thank you :)