5

anybody know how to convert a lead on update? It is for if the lead Email matches an existing Contact's email. I can get it to work on after insert, but when I try on update(before update or after update) I get this error:

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger 'leadFilter' caused an unexpected exception, contact your administrator: leadFilter: execution of BeforeUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00QR000000DGrtr) is currently in trigger leadFilterTrg, therefore it cannot recursively convert itself: []: Class.leadFilter.filterLeads: line 76, column 1

Class is like this:

for (Lead l : leadRecords){                                 
            if(contactEmailMap.containsKey(l.email)){
                system.debug('contactMap contains lead email: ' + l.email);
                Contact c = contactEmailMap.get(l.email);

            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(l.id);
            lc.setContactId(c.id);
            lc.setAccountId(c.accountId);
            lc.setDoNotCreateOpportunity(true);
            lc.setOwnerId(c.ownerId);
            lc.isSendNotificationEmail();
            lc.setConvertedStatus('Duplicate Lead');                    
            convertList.add(lc);                                            
        }

    }
    database.convertLead(convertList);
1
  • OP: Please change the accepted answer for this question to mine, as sfdcfox has admitted that his answer is incorrect. Aug 4, 2014 at 9:14

2 Answers 2

1

Convert it in a future method. You can only involve in record in a trigger in a single instance of an event. For example, an insert can recursively call an update, which can recursively call a delete. However, a convert is considered an update, so you can't call a convert in an update trigger. You'll need to convert it asynchronously, via a future method.

6
  • Just for curiosity? Is conversion not allowed within a trigger at execution? Can't he wrap his "Convert" by using the an IF statement IF(!Lead.IsConverted){...} ?
    – Double A
    Jul 11, 2014 at 14:17
  • 1
    @DoubleA OP is trying to convert the lead during an update (e.g. when changed to Status X). This isn't allowed directly, so you have to be asynchronous about it. His trigger will also need to consider the fact that it may run during the convert as well (at least, if it meets the lead conversion lifecycle conditions). After triggers are automatically executed, while before triggers can be turned off. In essence, it's likely they will need to account for this behavior.
    – sfdcfox
    Jul 11, 2014 at 16:16
  • You can convert a lead in an AFTER UPDATE trigger. Jul 11, 2014 at 16:31
  • @DavidSchach I don't think you can convert a lead in an update trigger without going asynchronously... that is why I asked the question, else I would have.
    – JayDP123
    Jul 11, 2014 at 18:57
  • thanks SFDCFOX I figured that was the way to go but wanted to be sure. I will also check that the Lead is not already converted in the @future method, cheers .
    – JayDP123
    Jul 11, 2014 at 19:01
5

You can convert leads in After Update triggers. Be sure that you have checked "Enforce Validation and Triggers from Lead Convert" in the Lead Settings page in setup.

http://www.x2od.com/2008/11/09/salesforce-order-of-execution (disclaimer: I wrote the page based on documentation and my own experience) says that it can happen. It also says so in the documentation at https://help.salesforce.com/HTViewHelpDoc?id=leads_convert.htm&language=en_US (look to the third paragraph from the end, I think) and https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm in the Salesforce documentation.

Some sample code from a method that takes Trigger.new as a parameter (a List of Leads):

for(Lead l : leads2evaluate){
      Database.LeadConvert lc = new database.LeadConvert();
        try{
          lc.setLeadId(l.id);
          lc.setDoNotCreateOpportunity(true);
          lc.setOverwriteLeadSource(false);
          //lc.setOwnerId(l.ownerid);
          lc.setSendNotificationEmail(false);
          lc.setConvertedStatus('Qualified');
          LeadsToConvert.add(lc);
        }catch(DmlException n){
          system.debug('No match: ' + n);
        }
      }
    try{  
      Database.LeadConvertResult[] lcr = Database.convertLead(LeadsToConvert, false);
     } catch(DmlException n){
      system.debug('ConvertError: ' + n);
    }
  }
1
  • 4
    I must have forgotten that info at some point, or I might not have known that specifically. I do try to keep current on techniques, but I do miss things on occasion. That's why we're a community! Thanks for the info.
    – sfdcfox
    Jul 11, 2014 at 20:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .