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
Jake AllenJake Allen 

Importing leads from csv will only run apex trigger on the first lead

We currently have apex code to convert any new lead to a contact under qualifying conditions after insert and after update, but whenever you either do a bulk import, or if you select multiple leads and change a field (ie, the owner), the trigger will only fire for the first lead in the selection and the rest will never be triggered. Process Builders and other automation will fire correctly, so it's limited to apex triggers exclusively at this point. I presume this is because the trigger looks at it as a single update or transaction, rather than seeing it as multiple leads being updated? Personally I see this as a potential flaw in Salesforce design, but I wanted to know if anybody else has come across this? Is there a setting I'm missing, or maybe a workaround I haven't thought of? I'm at the verge of creating a cronjob to automatically convert qualifying leads, which is something I'd rather avoid if at all possible.
csfdccsfdc
Hi Jake. Can you please post the trigger? Also, if the trigger calls an Apex class to handle the logic, will you please share that as well. 
Jake AllenJake Allen
Unfortunately I don't believe the code is going to be the issue here. In both cases (bulk upload and multi-select), there is only one log entry in the developer console, and this matches with the first lead that's successfully converted. Unfortunately I'm also bound by a NDA so I'd need to go through a few hoops to post the code on a public forum.
Jake AllenJake Allen
To clarify: If I upload 10 leads through bulk import, the code is only ran for the 1st lead. The trigger does not run or log as having run for the other 9 leads imported.
csfdccsfdc
I have a few questions for you. First, what makes you believe that the code is not the issue here? Without seeing it or a basic blueprint of what it's doing, it's hard to rule that out as being the source of the issue.
 
Second, when you say that you are inserting records through 'bulk import', what do you mean? Data import wizard, dataloader, an api, or something else? 

Third, in your code I'd imagine you have some sort of loop, such as a for loop as an example. If so, are you including system.debug() statements inside of the loop? 
Jake AllenJake Allen

As mentioned above, there is no log for the run. If the code was being run at all for those leads, there would be some sort of log entry for them within the developer console, but this isn't the case. I also have debug statements outside of any loops at the very beginning of the trigger that identify the lead being processed, so these should show if nothing else.

By "Bulk Import", I'm referring to the "Import" button on the lead page. This then imports as a Bulk Data Load Job (found in Setup).

csfdccsfdc
Jake, since I can't reference your code I went ahead and wrote something really quickly to see if I could get it working. The code below is automatically converting mulitple leads upon import (you can obviously customize to test updates as well, which I fully expect to work too). I tested this with anonymous Apex as well as with the Dataloader's batch operation. The bulk api is taking a long time to process in my instance both with Dataloader and the Data Import Wizard, so I cannot confirm those results at this moment. But I don't see any reason why this wouldn't work with the bulk api.

If you have questions / comments, please let me know.

Trigger
trigger Lead_Trigger on Lead (after insert, after update) {
    
    If ( Trigger.isAfter && Trigger.isInsert ) {
        AutoConvertLeads.LeadAssign(Trigger.new, Trigger.oldMap);
    }
    
    If ( Trigger.isAfter && Trigger.isUpdate ) {
        AutoConvertLeads.LeadAssign(Trigger.new, Trigger.oldMap);
        
    }
                                 
}
Apex Class
public class AutoConvertLeads {

    public static void LeadAssign(List<Lead> newLeads) {
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        
        List<Database.LeadConvert> lcList = new List<Database.LeadConvert>();
        for( Lead l : newLeads ){
            Database.LeadConvert lc = new database.LeadConvert();
            IF (l.IsConverted == false){
                lc.setLeadId(l.id);
                lc.setConvertedStatus(convertStatus.MasterLabel);
                lcList.add(lc);
            }
        }
        Database.Convertlead(lcList);
    }
}

Lastly, for your reference some of the resources I referenced on this are:

https://automationchampion.com/tag/auto-convert-lead-using-apex/
https://developer.salesforce.com/forums/?id=906F00000008vuPIAQ
csfdccsfdc
FYI the code above works with Bulk Import. I just had two bulk data load jobs successfully process multiple  lead records for lead conversion.

Another thing - I wouldn't use this in Prod without adding in some exception handling, possibly some email alerts or logging results to a Log__c custom object, etc. Let me know what you think. I'm curious to hear your feedback.
Jake AllenJake Allen
Well dang....I absolutely stand corrected! It looks like my assumptions about the Developer Console logging were invalid and can't be relied upon. After running your code above, it successfully converted without any issue, though it only showed one lead in the logging. I'm not sure why this is, but I'll be doing some more digging and get back with you later today.