Advertisement
f1lam3ntx0

Duplicate account trigger

Aug 21st, 2022 (edited)
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. The following Trigger will fires when we try to create the account with same name i.e Preventing the users to create Duplicate Accounts.
  2.  
  3.  
  4. Trigger duplicateAccTrigger on Account (Before Insert){
  5.   switch on Trigger.operationType {
  6.     when BEFORE_INSERT {
  7.     duplicateAccHelper.checkDuplicateAcc(Trigger.new);
  8.     }
  9.   }
  10. }
  11.  
  12. Public with sharing duplicateAccHelper {
  13.   public static void checkDuplicateAcc(List<account> accLst){
  14.   if(accLst != null || accLst.size() > 0){
  15.   for(Account acc : accLst){
  16.   List<Account> accCheck = [SELECT Id, name from account where name = acc.name AND rating= acc.rating];
  17.   if(accCheck.size() > 0 ){
  18.   acc.name.addError = 'Account is already been created';
  19.   }
  20.   }
  21.   }
  22.   }
  23. }
Tags: Triggers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement