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
sudha76sudha76 

Trigger to check Account Duplicates and restrict entries.

I have this existing Trigger which checks for Account Duplicates, based on the Account Name - City Name. This is so generic that if the user enters the Account Name slightly differently but the street address is also the same, this Trigger does not warn users and lets them create the Account.

trigger AccountCreateTrigger on Account (before insert) {
for (Account acc: Trigger.new)
     {Account [] accs = [SELECT Name FROM Account WHERE Name = :acc.Name LIMIT 1];
      if (accs.size() > 0 ) //if account already exists, block and show the information on the page
         {acc.addError('Account  \'' + accs[0].Name +  '\' already exists. Please use existing account or contact your Salesforce Administrator for assistance.'); }
     }
}

 

 

 

 

How do you restrict users from entering the duplicate Accounts?

What criterias or fields do you use to make sure that duplicate accounts are not entered?

 

Please advice.

trigger AccountCreateTrigger on Account (before insert) {
for (Account acc: Trigger.new)
     {Account [] accs = [SELECT Name FROM Account WHERE Name = :acc.Name LIMIT 1];
      if (accs.size() > 0 ) //if account already exists, block and show the information on the page
         {acc.addError('Account  \'' + accs[0].Name +  '\' already exists. Please use existing account or contact your Salesforce Administrator for assistance.'); }
     }
}
crop1645crop1645

First of all, you need to worry about insert and update (as user could change name to something already present).  

 

Please refer to this recipe: http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving for guidance

 

As to which fields should be used to avoid 'duplicates', that is application logic unique to your business. You have to avoid false positives though in your checks if you go beyond Account.name

Yoganand GadekarYoganand Gadekar

It is upto you to decide which fields u should use to check duplication.

Following piece of code should check if accounts name is duplicated. U can add more fields as per requirement demands

 

trigger Account_Duplicate_check on account(before insert, before update){

   Set<string> StringSet = New Set<string>();

 

   for(acount acc:trigger.new){

      StringSet.add(acc.name);

    }

    Set<String> strSet = New Set<String>();

    for(account acList: [Select id from account where name in:StringSet]){

          strSet(acList.name);

     }

   

     for(acount acc:trigger.new){

         if(strSet.contains(acc.name)){

            acc.adderror('Duplicate account found');

          }

     }

}

 

mark this as answer if  this post works for you.