You need to sign in to do that
Don't have an account?
PlatFormCloud
Hi Experts,
I have a functionality, where I donot want users to provide duplicate Email associated with Account. The below triggers works perfectly. If there ate 100K Account records are present in the SalesForce System, then will the below triggers work?
Apex Trigger: Need the Best solution, which can work for 100K records
Hi Experts,
I have a functionality, where I donot want users to provide duplicate Email associated with Account. The below triggers works perfectly. If there ate 100K Account records are present in the SalesForce System, then will the below triggers work?
/* Preventing users creation of duplicate in Salesforce*/ trigger PreventDuplicateNameAccount on Account (before insert, before update) { //Preparing Account names in Set from trigger.new Set<String> nameSet = new Set<String>(); for(Account acc : trigger.new){ nameSet.add(acc.Email__c); } // getting the list of accounts in database with the account name we entered ( trigger.new) List<Account> accList = new List<Account>( [select id,name, Email__c from Account where Email__c in: nameSet]); for(Account a : trigger.new){ if(accList.size() > 0 ) a.addError('Accounts Email already exists in your Organization with Email '+a.Email__c); } }
It will work fine for single record transactions fron UI without any issue, regardless of no of Accounts present in ur system.
I suggest below modified trigger for bulk operations.
Above code works fine if we insert only one account at a time(From UI). If we insert 10 Accounts in bulk, think only 2 are duplicates, then above code will throw error for all 10 records. But below modified code will throw error for only those 2 duplicate Accounts.
Thanks!
Refer below links for more idea on how to call Batch from Trigger,
http://www.cloudforce4u.com/2013/11/call-batch-apex-from-trigger.html
http://sfdcsrini.blogspot.com/2014/06/how-to-execute-batch-apex-using-apex.html