+ Start a Discussion
Nirupama SharmaNirupama Sharma 

Apex Trigger to avoid Duplicate Records upon creations and updation

Hello all

i am trying to Design a Trigger where it should throw an error upon duplicate record creation, i know this can be achived with Duplicate management, but i have Custom Lookup field(User) which is not showing in the matching rules, Hence i am trying to write trigger, basically i have 3 Custom fields User(Assignedto__c ) which is 
Lookup(User) and other fields are 
Quater_Year__c which is picklist and Month (
Month__c) which is picklist field, 

Please Help with the trigger
trigger contactDuplicatePreventer on Contact(before insert, before update) 
{
	Set<String> setEmailID = new set<String>();
	Set<Id> setContID = new set<ID>();
    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			setEmailID.add(Contact.Email);
			setContID.add(Contact.id);
		}
    }

	List<Contact> lstCOntact = [select id ,email from contact where email in :setEmailID and id not in :setContID ];
    Map<String, Contact> contactMap = new Map<String, Contact>();

	for(Contact cont : lstCOntact)
	{
		contactMap.put(cont.email, cont);
	}	

    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			if(contactMap.containsKey(Contact.Email))
			{
				Contact.Email.addError('A Contact with this email address already exists.');
			}
		}	
	}	
}

 
TechingCrewMattTechingCrewMatt
Hello, Veena. An alternative to this approach is to add a custom text field named "Assigned To (Text)" annd use a very simple trigger to populate that field. Then, you can use standard matching rules to manage duplicates.
TechingCrewMattTechingCrewMatt
On second thought, I think you can populate the field using Process Builder and the entire thing will be declarative.
Nithin Kumar 89Nithin Kumar 89
trigger AccountDuplicateCheck on Account (before insert,before update)
{

    if((trigger.isinsert || trigger.isupdate) && trigger.isbefore)
    {
        for(Account acc : trigger.new)
        {
           integer lstaccount=[select count() from Account where name=:acc.Name Limit 1];
            if(lstaccount > 0)
            {
                acc.adderror('duplicated record found with same name');
            }
        }
    }
}
Nirupama SharmaNirupama Sharma
@ Nithin Thank you very much
TechingCrewMattTechingCrewMatt
FYI, the code provided won't allow records to uploaded in bulk because there is a SOQL query in the loop. I would suggest that code is not used without understanding the implications. It will be difficult to maintain if you're not familiar with Apex.
Amit Kumar1Amit Kumar1
trigger AccountDupCheck on Account (before insert,before update,before delete) {
    list<Account> ll=Trigger.isDelete?Trigger.old:trigger.new;
    map<String,Account> ls1 = new map<String,Account>();
    for(Account aa:ll){
        ls1.put(aa.name,aa);
    }
    list<Account> ls = [select id from Account where name in:ls1.keySet()];
    if(ls.size()>0){
        for(Account a:ll){
            a.adderror('Duplicate',false);
        }
    }
}

 
TechingCrewMattTechingCrewMatt
Amit, that will throw an error on each Account if only one of the Accounts is a duplicate.