Sunday, May 24, 2020

Trigger to prevent creating duplicate accounts in salesforce

This post explains how to prevent creating duplicate accounts in Salesforce.


Trigger :


trigger Prevent_duplicacy on Account (before insert) {

  Set<string> str=new Set<string>();
    for(Account acc:trigger.new)
    {
        str.add(acc.name);
    }
    
    List<Account> acc= new List<Account>([select name from Account where name in:str]);
    
    for(Account a:trigger.new)
    {
        if(acc.size()>0)
        {
            a.adderror('error');

        }
    }
}




As I already have an account named Microsoft in my org.. When I am trying to create a new account with same the name it shows an error.


Thanks,

 Lovesalesforceyes



1 comment:

Get selected radio button value in LWC

This post explains how to get selected radio button in LWC. Below is demo of output : To use the radio button in LWC you have to use the  li...