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
k practicek practice 

Updating to a duplicate record in an account how to achieve Error?

PratikPratik (Salesforce Developers) 
Hi,

Please elaborate your question so community can help you.

Thanks,
Pratik
k practicek practice
Hi Pratik,
This is my requirement
1) When a new account is being created, test the account name against all other account names already in the system. If there is an exact match, then prevent the save and pop this alert:
> “Duplicate records are exist”

2)- When saving an existing account, on save, also run the test to look for an exact name match on the account name (ensure you don’t find the same account as the one being saved). In this situation, if there is an exact match, we will still allow the save, but we’ll pop this alert as a warning:
> “There is another Account with the same name as this one. Please navigate to the Account tab, and in the Tools section on that page, execute the Account Merge in order to clean up this duplicate. If you are not the account owner of both accounts, please contact the account owner(s) and ask them to do this merge.”

I tried Like below .How to apply second statement in below trigger help me...

trigger AccountDuplicateTrg on Account (before insert,after update) {
    List<Account> all=Trigger.New;
    List<Id> accids=new List<Id>();
    List<String>  listOfAccountnames=new List<String>();
    for(Account a1:all)
    {
        listOfAccountnames.add(a1.name);
        accids.add(a1.id);
        //accids.add(a1.id);
        //System.debug(accids);
    
    }
    List<Account> accs=[SELECT id,name FROM Account WHERE Name IN : listOfAccountnames AND Id IN : accids];
    System.debug(accs);
    Map<String,Account> Mp=new Map<String,Account>();
    
    for(Account a2:accs)
    {
        Mp.put(a2.name,a2);
        
    }
    
    for(Account a:Trigger.new){
        if(Mp.get(a.name) != null){
            a.addError('Duplicate records are exist');
        }
    }
     for(Account a3:accs)
    {
        Mp1.put(a3.Id,a3);
        
    }
     for(Account Oaccount:Trigger.new){
        if(Mp1.get(Oaccount.Id) != NULL){
            Oaccount.addError('There is another Account with the same name as this one. Please navigate to the Account tab, and in the Tools section on that page, execute the Account Merge in order to clean up this duplicate. If you are not the account owner of both accounts, please contact the account owner(s) and ask them to do this merge.');
        }
    }
}

please help me.............
PratikPratik (Salesforce Developers) 
Hi,

Please see the below sample code to identify the Duplicate contact on the basis of email. Same logic you can apply for the Accounts.

Trigger:
 
trigger dupcontact on contact(before insert, before update) {
    
    List<lead> leadlist= new list<lead>();
    
    for(contact c : trigger.new) {
    
        if(c.email!=null)
        leadlist = [select id from lead where email=:c.email];
        if(leadlist.size()>0)
        c.adderror('Duplicate email id with lead recod with ID : '+leadlist[0].id + ' already exists' );
    }

    
    
}

Thanks,
Pratik

P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.
k practicek practice
Hi pratik,
            URGENT: In my code first one completed how to apply second one in my code.

please help me...