You need to sign in to do that
Don't have an account?
Jake 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.
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?
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).
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
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.