You need to sign in to do that
Don't have an account?
Manish Kumar 23
Trigger for Duplicate check account
Hi All,
I have written a trigger to check the duplicate name of account:
trigger accountDuplicatePreventer on Account
(before insert, before update) {
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account : System.Trigger.new) {
// Make sure we don't treat an Name that
// isn't changing during an update as a duplicate.
if ((account.Name != null) &&
(System.Trigger.isInsert ||
(account.Name !=
System.Trigger.oldMap.get(account.Id).Name))) {
// Make sure another new account isn't also a duplicate
if (accountMap.containsKey(account.Name)) {
account.Name.addError('Another new account has the '
+ 'same Name.');
} else {
accountMap.put(account.Name, account);
}
}
}
// Using a single database query, find all the accounts in
// the database that have the same Name as any
// of the accounts being inserted or updated.
for (Account account : [SELECT Name FROM Account
WHERE Name IN :accountMap.KeySet()]) {
Account newAccount = accountMap.get(account.Name);
newAccount.Name.addError('A account with this Name '
+ 'already exists.');
}
}
now problem is I am getting"Developer script exception from Gmail : accountDuplicatePreventer : accountDuplicatePreventer: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.accountDuplicatePreventer: line 35, column 1"
I already have account name-"Manish compnay"
Now I want to insert another account name-"manish company"
In this case I am geeting above error.
Request your help.
I have written a trigger to check the duplicate name of account:
trigger accountDuplicatePreventer on Account
(before insert, before update) {
Map<String, Account> accountMap = new Map<String, Account>();
for (Account account : System.Trigger.new) {
// Make sure we don't treat an Name that
// isn't changing during an update as a duplicate.
if ((account.Name != null) &&
(System.Trigger.isInsert ||
(account.Name !=
System.Trigger.oldMap.get(account.Id).Name))) {
// Make sure another new account isn't also a duplicate
if (accountMap.containsKey(account.Name)) {
account.Name.addError('Another new account has the '
+ 'same Name.');
} else {
accountMap.put(account.Name, account);
}
}
}
// Using a single database query, find all the accounts in
// the database that have the same Name as any
// of the accounts being inserted or updated.
for (Account account : [SELECT Name FROM Account
WHERE Name IN :accountMap.KeySet()]) {
Account newAccount = accountMap.get(account.Name);
newAccount.Name.addError('A account with this Name '
+ 'already exists.');
}
}
now problem is I am getting"Developer script exception from Gmail : accountDuplicatePreventer : accountDuplicatePreventer: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.accountDuplicatePreventer: line 35, column 1"
I already have account name-"Manish compnay"
Now I want to insert another account name-"manish company"
In this case I am geeting above error.
Request your help.
why you use this code---
While your work is done through the above code.
You can refer below updated code: If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
Is there any that It should treat one.
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal