completed assignment
This commit is contained in:
31
force-app/main/default/triggers/ValidateAccount.trigger
Normal file
31
force-app/main/default/triggers/ValidateAccount.trigger
Normal file
@@ -0,0 +1,31 @@
|
||||
trigger ValidateAccount on Account (before insert) {
|
||||
Set<String> accountNames = new Set<String>();
|
||||
|
||||
for (Account account : Trigger.New)
|
||||
{
|
||||
if (accountNames.contains(account.Name))
|
||||
{
|
||||
account.Name.addError('Account name already exists!');
|
||||
}
|
||||
else {
|
||||
accountNames.add(account.Name);
|
||||
}
|
||||
}
|
||||
|
||||
List<Account> existingAccounts = [
|
||||
SELECT Id, Name
|
||||
FROM Account
|
||||
WHERE Name IN :accountNames
|
||||
];
|
||||
|
||||
for (Account account : Trigger.New)
|
||||
{
|
||||
for (Account existingAccount : existingAccounts)
|
||||
{
|
||||
if (account.Name == existingAccount.Name && account.Id != existingAccount.Id)
|
||||
{
|
||||
account.Name.addError('Account name already exists!');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user