0

I am trying to write a trigger on lead which gives result as Whenever i update a lead, that should be converted into account, contact and Opportunity. I wrote a trigger, but it is showing following error which shown in pic. and also please follow my code.

Trigger:

trigger LeadConvert_Trigger on Lead (after update) {

    for(Lead leed : Trigger.New){
        Database.LeadConvert lcon= new Database.LeadConvert();
        lcon.setLeadId(leed.Id);

        LeadStatus convertStatus=[Select id, MasterLabel from LeadStatus where isConverted=true];
        lcon.setconvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult result= Database.convertLead(lcon);
    }
}

Error

enter image description here

2
  • 1
    I think you should apply a check in your for loop that will check: if(!leed.isConverted) – Mr.Frodo Jun 8 '16 at 10:28
  • one more thing you have DML operation in for loop.Which is strictly not suggested.A similar example you can found in this link – Mr.Frodo Jun 8 '16 at 10:45
0

use this code:

trigger:

trigger LeadConvert_Trigger on Lead (after update) {

if(p.firstRun)
    {

  p.firstRun = false;

    for(Lead leed : Trigger.New){
        Database.LeadConvert lcon= new Database.LeadConvert();
        lcon.setLeadId(leed.Id);

        LeadStatus convertStatus=[Select id, MasterLabel from LeadStatus where isConverted=true];
        lcon.setconvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult result= Database.convertLead(lcon);
    }

    }

class:

public class p { 
   public static boolean firstRun = true; 
}
2
  • 1
    Like other answer and question your code also have DML in loop. Database.convertLead counted against the DML limits. – Mr.Frodo Jun 8 '16 at 10:49
  • why 'firstrun' check? – Mahmood Jun 8 '16 at 11:25
0

Ok. For some reason, after you convert the lead, salesforce edits it again (probably to set isConverted to true).

What you need to do is a slightly change on you trigger:

Trigger LeadConvert_Trigger on Lead (after update) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead leed: Trigger.new) {
          if (!leed.isConverted) {
               Database.LeadConvert lc = new Database.LeadConvert();

               lc.setLeadId(leed.Id);
               lc.setConvertedStatus(convertStatus.MasterLabel);

               leadConverts.add(lc);
          }
     }

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

Thanks, @Mahmood for providing the link. Also, please check deadlypenguin's blog for a more thoroughly explanation.

12
  • Using SOQL inside for loops is not good practice, and is gonna hit the governor limits. Have that in a list outside of the loop and use the list instead in the loop to set values. – Mahmood Jun 8 '16 at 10:40
  • Oh. True. Great for catching that. – Fernando Gavinho Jun 8 '16 at 10:41
  • Database.convertLead is a DML, and you dont want that inside your loop as well. This is as dangerous as using SOQL inside loops and prone to hitting governor limits. – Mahmood Jun 8 '16 at 10:51
  • @Mahmood. I looked, but couldn't find a way to mass convert lead. If all conversions has to be made one by one, I don't see another way to do that. – Fernando Gavinho Jun 8 '16 at 12:35
  • 1
    Hi @Fernando Gavinho, please follow this link blog.deadlypenguin.com/blog/2014/07/23/… – KS Kumaar Jun 8 '16 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.