+ Start a Discussion
alok29novalok29nov 

Account having same name with same parent id can't be inserted

Hi All,

 

I have a requirement where I need to create a trigger which ensures an account having same name with same parent id can't be inserted.

 

Can somebody help  me with the code?

 

 

Thanks in advance!

 

~Alok

Navatar_DbSupNavatar_DbSup

Hi,

 

You can try below code

trigger duplicateaccount on Account (Before insert)
{
      Account a=trigger.new[0];
      list<account>a1=[select id from account where name=:a.name ];
      
        if(a1.size()>0)
        {
            a.Name.addError('Another new acconut has the ' + 'same name.');
        }
    
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

alok29novalok29nov

Hi,

 

Thanks for your reply!

 

My requirement is to throw an  error only when parent id and name of a new account  both is same.

 

Thanks

Alok

Ritesh AswaneyRitesh Aswaney

That can be accomplished with just a minor tweak to the SOQl Query in the response above (Not this isnt a buklified trigger)

 

[select id from account where name=:a.name And Id = :a.ParentId ];
alok29novalok29nov

Hi Ritesh,

 

I wanted a bulkified trigger I tried though but getting one error or other. I will see how I can accomplosih this.

 

Thanks

Alok