You need to sign in to do that
Don't have an account?
Dilip Kulkarni
trigger( prevent duplicates) on account object.
Hi Experts,
I need to create a trigger on account object which will prevent the users in creating duplicate account based on the name.
Kindly provide me the same.
Regards.
I need to create a trigger on account object which will prevent the users in creating duplicate account based on the name.
Kindly provide me the same.
Regards.
BestRegards,
Dayakar.D
All Answers
Please try below trigger.
Please let me know if it works.
BestRegards,
Dayakar.D
Hope this will help you!
Your trigger works fine. Can I put (before insert,before update) for the same?
Your trigger is giving message on account as:
Apex trigger AccountDuplicateTrigger caused an unexpected exception, contact your administrator: AccountDuplicateTrigger: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.AccountDuplicateTrigger: line 13, column 1.
Instead of 'You cannot create a dulplicate account'.
BestRegards,
Dayakar.D
Thank you very much. Could you please elaborate me the logic in this trigger in detail,as I am new to apex coding.
Please find code comments, BestRegards,
Dayakar.D
What will be the test class for the trigger below:
trigger AccountDuplicateTrigger on Account (before insert) {
list<string> accountNames=new list<string>();
for(Account accountVar:trigger.new)
{
accountNames.add(accountVar.name);
}
//getting list of accounts with given name
list<Account> listOfDuplicateAccounts=[select id,name from Account where name in:accountNames];
for(Account account:trigger.new)
{
if(listOfDuplicateAccounts.size()!=0)
{
account.addError('Account already exists with this name');
}
}
}
Here is test class for the trigger.
Please let me know, if you have any issues.
BestRegards,
Dayakar.D
I got 100% code coverage.Thanks for your kind help.
Sample Code :
trigger AccountDuplicateTrigger on Account (before insert,before update) {
for(Account a:Trigger.new)
{
List<Account> acc=[select ID from account where Name=:a.Name ];
if(acc.size()>0)
{
a.name.adderror('You cannot create a dulplicate account');
}
}
}
Below post can be used to avoid Duplicate Fields by using trigger :
Here i have used Author object and Email field for my requirement .Please use your Sobject and field according to your requirement in the place of Email
Please find the below post further information:
Avoid Duplicate for Insert Operation By Using Apex Trigger (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex.html)
And the below Post explaines how to avoid Duplicate Fields by updating the above code :
Avoid Duplicates for both insert and Update Operation (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex_21.html)
I have created insert and update operation seperately to make you understand Clearly.
Please let me know if you require any further information
Please find the below post ,Which explains more about avoiding duplicate records .
Duplicate Records (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex_21.html)
Please make use of below post for more hands on examples of apex trigger .
Apex Trigger Examples (https://salessforcehacks.blogspot.com/2020/02/apex-trigger-examples-salesforce-apex.html)
//Preparing Account names in Set from trigger.new
Set<String> nameSet = new Set<String>();
for(Account acc : trigger.new){
nameSet.add(acc.name);
}
// getting the list of accounts in database with the account name we entered ( trigger.new)
List<Account> accList = new List<Account>([select id,name from Account where name in: nameSet]);
for(Account a : trigger.new){
if(accList.size() > 0 )
a.addError('Account already exists in your Organization with name '+a.name);
}
}
trigger AccountTrigger on Account (before insert) {
If(trigger.isInsert && trigger.IsBefore){
Set<string> AccNames = new Set<string>();
for( account acc : trigger.new){
AccNames.Add(acc.Name);
}
List<account> DuplicateAccounts = [Select id,Name from Account where Name In :AccNames ];
If (DuplicateAccounts.size()>0)
{
for( account acc : trigger.new){
for(account dupAcc:DuplicateAccounts){
if(acc.name== dupAcc.Name && acc.Name!=null){
acc.Name.addError('Error: Record already exist with the name -'+acc.name);
}
}
}
}
}
}