-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrigger-08.cls
More file actions
25 lines (25 loc) · 800 Bytes
/
Trigger-08.cls
File metadata and controls
25 lines (25 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
The following Trigger will fires when we try to create the account with same
name i.e. Preventing the users to create Duplicate Accounts
*/
trigger acctrigger on Account (before insert,before update) {
Set<String> names=new Set<String>();
for(Account a:Trigger.new){
names.add(a.name);
}
List<Account> acc = new List<Account>([Select Id,name from Account where name in:names]);
for(Account a:Trigger.new){
if(trigger.isinsert){
if(acc.size()>0){
a.addError('Duplicate record exists');
}
}
if(trigger.isupdate){
for(Account old:Trigger.old){
if(acc.size()>0 && a.name!=old.name){
a.addError('Duplicate record exists');
}
}
}
}
}