You need to sign in to do that
Don't have an account?

Creating a Trigger on Account object
Hi
I have an requirement when i am trying to insert account with same name which is alredy exists then it prevents the creation of duplicate record
I have written a code but i am getting error "Initial term of field expression must be a concrete SObject: List<Account>"
Please chech my code
trigger Tsen2 on Account (before insert,before update) {
for(Account a:Trigger.new){
list<Account> acc=[select id from Account where name=:a.Name and Rating=:a.Rating];
if(acc.size()>0){
acc.Name.addError('you cannot create duplicate values');
}
}
}
Thanks in Advance
I have an requirement when i am trying to insert account with same name which is alredy exists then it prevents the creation of duplicate record
I have written a code but i am getting error "Initial term of field expression must be a concrete SObject: List<Account>"
Please chech my code
trigger Tsen2 on Account (before insert,before update) {
for(Account a:Trigger.new){
list<Account> acc=[select id from Account where name=:a.Name and Rating=:a.Rating];
if(acc.size()>0){
acc.Name.addError('you cannot create duplicate values');
}
}
}
Thanks in Advance
All Answers
Please try to update your code like below:- NOTE:- Adding SOQL inside the for loop is not a good pratice
Please let us know if this will help you
Thanks
AMit Chaudhary
{
AccountTriggerHandler objAccountHandler = new AccountTriggerHandler();
if(Trigger.isBefore && Trigger.isInsert)
objAccountHandler.onBeforeInsert(Trigger.New);
else if(Trigger.isBefore && Trigger.isUpdate)
objAccountHandler.onBeforeUpdate(Trigger.oldMap, Trigger.NewMap);
}
public with sharing class AccountTriggerHandler
{
public AccountTriggerHandler()
{
}
public void onBeforeInsert(List<Account> lstNewAccount)
{
preventDuplicates(new Map<Id,Account>(), lstNewAccount);
}
public void onBeforeUpdate(Map<Id,Account> mapOldAccount, Map<Id,Account> mapNewAccount)
{
preventDuplicates(mapOldAccount, mapNewAccount.values());
}
private void preventDuplicates(Map<Id,Account> mapOldAccount, List<Account> lstNewAccount)
{
Map<String,Account> mapStrNameRatingToAccount = new Map<String,Account>();
Set<String> setRating = new Set<String>();
Set<String> setName = new Set<String>();
// Collect All Names from Trigger.New
for(Account objAccount : lstNewAccount)
{
/* 1. On insert
OR
2. On update but only if Name or Rating is changed
*/
if(Trigger.isInsert ||
(Trigger.isUpdate &&
(mapOldAccount.get(objAccount.Id).Name != objAccount.Name
||
mapOldAccount.get(objAccount.Id).Rating != objAccount.Rating
)
)
)
{
setRating.add(objAccount.Rating);
setName.add(objAccount.Name);
mapStrNameRatingToAccount.put(objAccount.Name + '__' + objAccount.Rating, objAccount);
}
}
// iterate over matching Account and add error
for(Account objMatchingAccount : [SELECT Id,Name
FROM Account
WHERE Name IN:setName AND Rating IN:setRating])
{
String strNameRatingPair = objMatchingAccount.name + '__' + objMatchingAccount.Rating;
if(mapStrNameRatingToAccount.containsKey(strNameRatingPair))
mapStrNameRatingToAccount.get(strNameRatingPair).addError('You cannot create duplicate values!');
}
}
}
any one help me