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
piyush mittal 16piyush mittal 16 

how to check duplicate records using triggers??

Please share your answers
Raj VakatiRaj Vakati
You can do it like this 

https://developer.secure.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
 
trigger leadDuplicatePreventer on Lead
                               (before insert, before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
    for (Lead lead : System.Trigger.new) {
		
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
    
        if ((lead.Email != null) &&
                (System.Trigger.isInsert ||
                (lead.Email != 
                    System.Trigger.oldMap.get(lead.Id).Email))) {
		
            // Make sure another new lead isn't also a duplicate  
    
            if (leadMap.containsKey(lead.Email)) {
                lead.Email.addError('Another new lead has the '
                                    + 'same email address.');
            } else {
                leadMap.put(lead.Email, lead);
            }
       }
    }
	
    // Using a single database query, find all the leads in  
    
    // the database that have the same email address as any  
    
    // of the leads being inserted or updated.  
    
    for (Lead lead : [SELECT Email FROM Lead
                      WHERE Email IN :leadMap.KeySet()]) {
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email '
                               + 'address already exists.');
    }
}

 
piyush mittal 16piyush mittal 16
is there any other method ??
Raj VakatiRaj Vakati
You can able to do it with salesforce duplicate rules also 

Refer this link 

https://help.salesforce.com/apex/HTViewHelpDoc?id=duplicate_rules_map_of_reference.htm
piyush mittal 16piyush mittal 16
Thank you man
Raj VakatiRaj Vakati
Close this thread if you dnt need any additional info