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
raghava520raghava520 

Hi experts i want to write a trigger using before insert on account object if we are insert 100 account records if the account is having the same name as existing account it should skipped and remaining 99 should be saved in the database

Boris BachovskiBoris Bachovski
I'm afraid you can't do that selection on before insert trigger. All records will get inserted regardless, unless you have a validation rule etc.

The solution would be to do delete the accounts that have a duplicate name in the system but after they're inserted, something like this:
 
trigger Account on Account (after insert)
{
	Set <String> accountNamesInTrigger = new List <String> ();
	List <Account> accountsToDelete = new List <Account> ();

	for (Account account : trigger.new)
	{
		accountNamesInTrigger.add(account.Name);
	}

	for (Account existingAccount : [SELECT Id FROM Account WHERE Name IN :accountNamesInTrigger AND Id NOT IN :trigger.newMap.keySet()])
	{
		accountsToDelete.add(existingAccount);
	}

	delete accountsToDelete;
}
I haven't compiled or tested this but with some minor tweaks or typo fixed you should be able to get it going.
 
BalajiRanganathanBalajiRanganathan
You can try below code from the post https://developer.salesforce.com/forums/ForumsMain?id=906F00000008zMpIAI
trigger AvoidDuplicateAccounts on Account (before insert) {
  Set<String> setAccountName = new Set<String>(); 

  for (Account objAccount: [Select Name from Account])
    setAccountName.add(objAccount.Name);

  for(Account objAccount: Trigger.new) {
      if(!setAccountName.contains(objAccount.Name))      {
	  setAccountName.add(objAccount.Name);
      } else {
	objAccount.Name.addError('Account with same name Exists');
      }
    }
}
Boris BachovskiBoris Bachovski
Balaji, wouldn't that stop the trigger and roll back all records that it's processing?
BalajiRanganathanBalajiRanganathan
it won't if data is imported using tools or using DML operation with  allOrNone set false.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_exceptions.htm
admin sureshadmin suresh
Hi raghhava,

here is the solution preventing duplicate account name and inserting remaining accounts

trigger preventduplicate on account (before insert)
{

  list<account> accountlist = new list<account>();
   list<account> acclist = trigger.new;
   list<account> accold = [select id, name from account];
   for(account a :acclist)
   { 
        for(account old : accold)
        {
            if(old.name==a.name)
           {
             system.debug(' duplicate account name');
              break; 
           } 
           else
             {
               accountlist.add(a); 
             }
        }
}
insert accountlist;
}
BalajiRanganathanBalajiRanganathan
Hi Borris, i think your solution delete the existing records instead of the one which are just inserterd. also it does not prevent duplicates with in the list of new records inserted. But Raghava want the new dublicate accounts to be skipped.