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
PlatFormCloudPlatFormCloud 

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);

        }
}

 
Nagendra ChinchinadaNagendra Chinchinada
Of course it will work. Your query is whether will it hit governer limits?. Only Select query from Account will fall under governer limts in your code. It will work, if u insert Accounts in bulk upto 50K at a time. It will fail, hit governer limit after 50K(in bulk).
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.
/* 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]);
    
    Set<String> emailSet = new Set<String>();
    
    for(Account acc : [select id,name, Email__c from Account where Email__c in: nameSet])
        emailSet.add(acc.Email__c);
    
    
    
    for(Account a : trigger.new){
        if(emailSet.contains(a.Email__c))
            a.addError('Accounts Email already exists in your Organization with Email '+a.Email__c);
        
        //    if(accList.size() > 0 )
        //       a.addError('Accounts Email already exists in your Organization with Email '+a.Email__c);
        
    }
}



 
PlatFormCloudPlatFormCloud
This is truely a nice catch dear. However, is there any way to handle more than 50K data?

Thanks!
Nagendra ChinchinadaNagendra Chinchinada
Above methode fails if ur inserting records have more than 50K duplicates. This can be handled by a Batch Job. Code a batch Apex job and call it from ur trigger if inserting records count is morethan 50K.

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