+ Start a Discussion
RPReddyRPReddy 

Duplicate Record

i wrote a trigger on the Account object.It validates the First name for find out the duplicate record .once first names are same it throws an error like already exist.my question is suppose as per requirement  i want to change my rest of fields apart from First Name.i was trying to change the rest of fields and save the record,it throws an same error like already exist.can you tell me how to write a trigger to  save the record without affecting the first name and updating the rest of the fields

 

trigger duplicate on Account (before insert,before update) {
set<string> setnames=new set<string>();
list<account> listnames=new list<account>();
for(account acc:trigger.new)
{
setnames.add(acc.name);
}
listnames=[select name,id from account where name in:setnames];
for(account acc:trigger.new)
{
for(account dup:listnames)
{
if(acc.name==dup.name)
acc.adderror('already exist');

}

}

}

Marco_MaltesiMarco_Maltesi

hi!

 

i think that it s better to create a field of verification (unique) and add a  wf (Name check when account .name diff  NULL) who put the account .name in the verification field.

i do it and it work very well.

excuse my English am french!

hope that helps! 

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet as reference:

 

trigger duplicate on Account (before insert,before update)

{

set<string> setnames=new set<string>();

 

list<account> listnames=new list<account>();

 

for(account acc:trigger.new)

{

setnames.add(acc.name);

}

listnames=[select name,id from account where name in:setnames];

 

if(trigger.isinsert)

{

for(account acc:trigger.new)

{

for(account dup:listnames)

{

if(acc.name==dup.name)

acc.adderror('already exist');

}

 

}

}

if(trigger.isupdate)

{

for(account acc:trigger.new)

{

for(account dup:listnames)

{

if(acc.name==dup.name && acc.name !=trigger.oldmap.get(acc.id).name)

acc.adderror('already exist');

}

 

}

}

 

}

 

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

praveenpropraveenpro
trigger CheckOppname on Opportunity (before insert, before update) {

 for (Opportunity opp :Trigger.new ) {

  Integer oppCnt = [Select Count() from Opportunity where Name = :opp.Name];
  if (oppCnt > 1) opp.addError('Duplicate opportunity name found.');
 }

}