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
rajesh k 10rajesh k 10 

URGENT:How to achieve below trigger?

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 account record exist”

- 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.”

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

Please refer to below trigger, its' basically to identify the duplicate contacts based on the contact email. 
You can modify this trigger as per your requirement.


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


rajesh k 10rajesh k 10
Hi Pratik,
                    I want second one Example.This i know in google.Second one i think not possible.If second one possible give me solution?
Hi Rajesh,
To achieve second requirment, you can write logic in before update Trigger as per your requirement. This will ensure that logic is executed only for existing accounts and this will not find the same account as the one being saved.  And it is not directly possible to diaplay warning message via apex trigger. I have found below article that might help you in achieving warning message.
rajesh k 10rajesh k 10
Hi Sir ,
               Thank you for reply i am new  in salesforce.

How to achieve my Trigger?

please help me with code Sir. 

rajesh k 10rajesh k 10
Fot insert i tried below code ,Updation time please help me...

trigger AccountDuplicateTrg on Account (before insert) {
    List<Account> all=Trigger.New;
    List<String>  ls=new List<String>();
    for(Account a1:all)
    List<Account> accs=[SELECT id,name FROM Account WHERE Name IN : ls];
    Map<String,Account> dm=new Map<String,Account>();
    for(Account a2:accs)
        if(dm.get( != null){
            a.addError('Alredy duplicate account Exist ');

help me sir...