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
ramakoti reddyramakoti reddy 

whenever new record is created into Account object , before this new record is inserted delete the accounts with the same name.

give trigger code
Ajay K DubediAjay K Dubedi
Hi Ramakoti,

Please try the below code :

trigger accountrecords on Account (before insert) 
{
    List<String> myname=new List<String>();
    for(Account a:trigger.new)
    { 
        myname.add(a.name);
    }
    List<Account> myAccountList = new List<Account>();
    myAccountList = [select id,name from Account where name in:myname];
    system.debug('myAccountList'+myAccountList);
    if(myAccountList != Null && myAccountList.size()>0)
    {
        
        delete myAccountList;  
    }
    
}

Hope this helps.

Thank You
Ajay Dubedi
Akshay_DhimanAkshay_Dhiman
Hi Ramakoti reddy,

Try this code Its working fine.

 
trigger AccDeleteSameName on Account (before insert) {
    List<String> accName = new List<String>();
    for(Account a :Trigger.New){
        accName.add(a.Name);
    }
    if(accName.size() > 0){
        List<Account> accList = [select name from Account where Name IN:accName];
        if(accList.size() > 0){
            delete accList;
        }
    }       
}


        
  if you found this answer helpful then please mark it as best answer so it can help others.      
  
  Thanks 
  Akshay
sowmya Inturi 9sowmya Inturi 9
Hi,
There might be a case where you have a chance of updating the account name with the same name of another accounts. So it would be better if you also add before update in the trigger.

Thanks,
Sowmya