function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
econ242econ242 

Trigger to auto-convert Lead when "Qualified" not working...and I know it's me! :)

Hi all,

Looking for some assistance here...I have a requirement to have a Lead automatically convert to an Account and Contact (no opportunity needed) when the Lead status is set to "Qualified". I also have an existing trigger on "Task" that when a particular picklist value is selected and the Task is marked "Completed" it will automatically change the Lead status to Qualified...that trigger works perfect in my Sandbox.

I've searched tons of articles on here about a trigger to auto-convert a Lead based on certain values and used the below code as suggested:

trigger LeadConvert on Lead (after insert,after update) {
  //Bulkified
  List<String> LeadNames = new List<String>{};
  for(Lead myLead: Trigger.new){
   if((myLead.isconverted==false) && (myLead.status == 'Qualified')) {
  Database.LeadConvert lc = new database.LeadConvert();
          lc.setLeadId(myLead.Id);
          lc.convertedStatus = 'Qualified';
          //Database.ConvertLead(lc,true);
          lc.setDoNotCreateOpportunity(true);
          Database.LeadConvertResult lcr = Database.convertLead(lc);
          System.assert(lcr.isSuccess());
          }
          }
  }

Issue: When I complete a Task that fires the trigger to change the Lead status to Qualified, it says the Lead was converted, but when I go back to check, the Lead was NOT converted AND the Task does not show up in the activity history AND the Lead Status is unchanged (not showing as "Qualified") even though it should per the Task trigger...

I'm totally lost on this one...any help would be great...thanks in advance!!!
bob_buzzardbob_buzzard
This sounds more like the task trigger isn't working as expected.  If the lead status is not changed to 'Qualified' then your lead trigger will take no action.  Can you post the task trigger.
econ242econ242
Thanks for your response Bob...below is the code:

trigger changeLeadStatustask2 on Task (before insert, before update) {
    List<Lead> LeadToUpdate = new List<Lead>();
        for (Task tsk: Trigger.new){
            if(tsk.Status=='Completed' && tsk.Subject=='Call to Qualify (Successful)' && 
           tsk.WhoId != null &&
           String.valueOf(tsk.WhoId).startsWith(Schema.SObjectType.Lead.getKeyPrefix())){
                Lead ld = new Lead(Id=tsk.whoid);
                   ld.Status = 'Qualified';            
                   ld.Stage__c = 'Qualified - Convert to QP';
                LeadToUpdate.add(ld);          
            }
          
        }      
        if(!LeadToUpdate.isEmpty()){
            try{
                update LeadToUpdate;
            }
            catch(DMLException e){
                for(Task tsk: Trigger.new){
                    tsk.addError(e.getDmlMessage(0));
                }
            }
        }
}
bob_buzzardbob_buzzard
I can't see anything obviously wrong - have you tried adding debug statements and turning on debug logging?  What seems most likely is that you aren't adding any leads to the LeadToUpdate list, but I think you'll need to debug that to figure out exactly why.
econ242econ242
I'm very new to this so I'm not well versed in writing debug statements yet...unfortunately.  I'll see if I can figure it out...

Thanks for your help Bob!!!