Wednesday, June 8, 2016

Prevent duplicate Account using Trigger

This is the trigger i have wrote to prevent the creation of duplicate Accounts based on Account Name.


trigger preventDuplicateAccount on Account(before insert,before update){
set<string> accountSet = new set<string>(); 
Map<string,Account> accountMap = new Map<string,Account>();
for(Account ac:trigger.new){
    accountSet.add(ac.name);
}

for(Account a:[SELECT id,name from Account where Name IN :accountSet]){
     accountMap.put(a.Name,a);
}
for(Account account:trigger.new){
    if(accountMap.containsKey(account.name)  ){
       //show error msg on the Account Name field
         account.Name.addError('Account is Already Exist');
       }
}
}

1 comment:

  1. Thank you for sharing wonderful information with us to get some idea about that content.
    CPQ Certification
    CPQ Certification Salesforce

    ReplyDelete

Common interview questions for Salesforce developers

Name three Governor Limits .                                                                                    Description Synchronous ...