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
newbie@newnewbie@new 

Converting Lead through Apex code

Hi All 

I am facing a issue while converting a lead through Trigger 
my Requirement is lead should be automatically Converted whenever lead status is Qualified 
Below is the error i am getting :

Error i am getting




Code that i am using is 
trigger web2LeadConvert on Lead (After Update ) 
{
    Database.LeadConvert[] converts = new Database.LeadConvert[0];
    for(Lead record: Trigger.new) {
        if(record.Status == 'Qualified') 
        {
            Database.LeadConvert thisLead = new Database.LeadConvert();
            thisLead.setLeadId(record.Id);
            thisLead.setConvertedStatus('Qualified');
            converts.add(thisLead);
            System.debug('@@@ It has @@'+converts);
        }
    }
    System.debug ('@@Convertssss'+ converts.isEmpty());
    if(!converts.isEmpty()) {
        Database.convertLead(converts);
        System.debug('@@@converts'+converts);
    }
}
Please help me ... 
where am i doing the mistake ...
 
NagendraNagendra (Salesforce Developers) 
Hi,

Sorry for this issue you are encountering.

May I Suggest you please check with below link from stack exchange community with a similar discussion which might help you further. Please let us know if this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra
v varaprasadv varaprasad
Hi ,

Please check once below code : 
Trigger web2LeadConvert on Lead (after Update) {
    
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.Status == 'Qualified') {
               Database.LeadConvert lc = new Database.LeadConvert();
              
               
               lc.setLeadId(lead.Id);
              
               lc.setConvertedStatus('Qualified');
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}

Hope it helps you.
Please let me know in case of any other help.


thanks
Varaprasad