+ Start a Discussion
k sfdck sfdc 

Using trigger how to check duplicate account name in insert and updation time in salesforce?

 
Vijay NagarathinamVijay Nagarathinam
Hi,

​Try this code it will be work and it is bulkified 
trigger duplicateAccName on Account (before insert,before update)
{
Map<string,Account> accMap = new Map<string,Account>();
set<string> accSet = new set<string>();
    for(Account acc : Trigger.new)
        accSet.add(acc.name);
    for(Account a : [select id,name from Account where name in : accSet])
        accMap.put(a.name,a);
    for(Account acc1 : trigger.new)
        {
        if(accMap.containskey(acc1.name))
        acc1.addError('Account name Already Exist!!');
        }
}

 
k sfdck sfdc
Hi,
          I want two different Error messages before insert time  and before update time how?
MithunPMithunP
Hi k sfdc,

I have Updated Vijay's code for different error messages for Insert and Update.
trigger duplicateAccName on Account (before insert,before update)
{
Map<string,Account> accMap = new Map<string,Account>();
set<string> accSet = new set<string>();
    for(Account acc : Trigger.new)
        accSet.add(acc.name);
    for(Account a : [select id,name from Account where name in : accSet])
        accMap.put(a.name,a);
    for(Account acc1 : trigger.new)
        {
        if(accMap.containskey(acc1.name))
           if(Trigger.isInsert){
             acc1.addError('Account name Already Exist For Insert!');
           }
           else if(Trigger.isUpdate){
             acc1.addError('Account name Already Exist For Update!!'); 
           }
        }
}
Best Regards,
Mithun.
 
MithunPMithunP
Hi k sfdc,

I thing Warning is not possible currently, we can display it as error only.

Best Regards,
Mithun.
k sfdck sfdc
Hi,
     Using below link is possible updation time show alert and allow save(already record exist in database) functionality .

http://www.isfsolutions.com/salesforce-apex-message-box/

help me...
k sfdck sfdc
I think Create vf page(with one boolean varible) and add this vf page to Account detail page when edit and save check whether duplicate account exist display alert using vf page and allow save otherwise without error allow save functionality.

help ...
MithunPMithunP
Hi,

Did you tried that workaround, if so where did you stuck, what is the issue.

Best Regards,
Mithun.
 
Mac SwiftMac Swift
Hi @MithunP Thank you for the amazing code. But while deploying it into the production from the sandbox (via change set) I got code coverage error. I am new to the developer side. Please help me to overcome this error.