You need to sign in to do that
Don't have an account?
Aaron Hill
Long Trigger Slowing down Inline Edits and batch updates, how can I refactor to increase speed?
Hi Everyone,
So a little background info. We created a relationship between leads and accounts through a lookup field on leads called "Related Account". I wanted leads to be assigned to accounts based on various criteria in a particular order. I initially wrote the trigger (with a significant amount of help from this community) to match a lead to an account based on company name. And then, using the same syntax from that prototype added three more criteria, so if there wasn't a match based on company name it would match secondly on LinkedIn Company ID, Thirdly on matching the lead email domain with the account website (I parsed it up to the beginning of the domain), and Fourthly on matching the lead email website with the account website.
It works fine but the problem is- it's super slow. Like the load time is increased by 4 or 5 more seconds on average. Also bulk uploads can take up to 30-40 minutes. This is not ideal. Lately I've been disabling the trigger just before a bulk upload and re-enabling it afterwards. My question- is there a way that I can refactor my code so that it runs faster? I use a lot of lists, so I'm thinking that might have something to do with it.
Here's my code:
I'd appreciate any feedback! Thanks!
Best,
Aaron
So a little background info. We created a relationship between leads and accounts through a lookup field on leads called "Related Account". I wanted leads to be assigned to accounts based on various criteria in a particular order. I initially wrote the trigger (with a significant amount of help from this community) to match a lead to an account based on company name. And then, using the same syntax from that prototype added three more criteria, so if there wasn't a match based on company name it would match secondly on LinkedIn Company ID, Thirdly on matching the lead email domain with the account website (I parsed it up to the beginning of the domain), and Fourthly on matching the lead email website with the account website.
It works fine but the problem is- it's super slow. Like the load time is increased by 4 or 5 more seconds on average. Also bulk uploads can take up to 30-40 minutes. This is not ideal. Lately I've been disabling the trigger just before a bulk upload and re-enabling it afterwards. My question- is there a way that I can refactor my code so that it runs faster? I use a lot of lists, so I'm thinking that might have something to do with it.
Here's my code:
trigger updateRelatedAccount on Lead (before update, before insert) { //you NEED before insert too set<string> accNames = new set<string>(); // Set for company name set<Decimal> accLIDs = new set<Decimal>(); // Set for Company ID set<String> accEmails = new set<String>(); // Set for account emails set<String> accWebs = new set<String>(); //Set for account websites for(Lead ld : Trigger.New){ accNames.add(ld.Company); accLIDs.add(ld.Company_ID__c); accEmails.add(ld.Lead_Email_Domain_Parsed__c); accWebs.add(ld.Lead_Website_Parsed__c); } list<Account> accs = new list<Account>([Select Id,Name,LinkedIn_Id__c,Account_Website_Parsed__c from account where name in :accNames or LinkedIn_Id__c in :accLIDs or Account_Website_Parsed__c in :accEmails or Account_Website_Parsed__c in :accWebs]); //using WHERE IN map<String,id> mapaccs = new map<String,id>(); map<Decimal, id> mapids = new map<Decimal, id>(); map<String, id> mapemails = new map<String, id>(); map<String, id> mapwebs = new map<String, id> (); for(Account ac : accs){ mapaccs.put(ac.Name,ac.Id); mapids.put(ac.LinkedIn_Id__c,ac.Id); mapemails.put(ac.Account_Website_Parsed__c, ac.Id); mapwebs.put(ac.Account_Website_Parsed__c, ac.Id); } for(Lead lds : Trigger.New){ //you new lead has company id, check if lead has same id if (lds.Company_ID__c !=null && mapids.get(lds.Company_ID__c)!=null) { lds.Related_Account__c = mapids.get(lds.Company_ID__c); //if so, copy id } else if (lds.Company != null && mapaccs.get(lds.company) != null) { //if not, search by name lds.related_account__c = mapaccs.get(lds.company); } else if (lds.Lead_Email_Domain_Parsed__c != null && mapemails.get(lds.Lead_Email_Domain_Parsed__c) != null) { lds.Related_Account__c = mapemails.get(lds.Lead_Email_Domain_Parsed__c); } else if (lds.Lead_Website_Parsed__c != null && mapWebs.get(lds.Lead_Website_Parsed__c) != null) { lds.Related_Account__c = mapWebs.get(lds.Lead_Website_Parsed__c); } else { lds.Related_Account__c = null; } } }
I'd appreciate any feedback! Thanks!
Best,
Aaron