+ Start a Discussion
rajesh k 10rajesh k 10 

URGENT:How to achieve below trigger?

Hi,
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:

 
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
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?
rmittal85rmittal85
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.

http://www.isfsolutions.com/salesforce-apex-message-box/
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)
    {
        ls.add(a1.name);
    
    }
    List<Account> accs=[SELECT id,name FROM Account WHERE Name IN : ls];
    Map<String,Account> dm=new Map<String,Account>();
    
    for(Account a2:accs)
    {
        dm.put(a2.name,a2);
    }
    
    for(Account a:Trigger.new){
        if(dm.get(a.name) != null){
            a.addError('Alredy duplicate account Exist ');
        }
    }
}


help me sir...