You need to sign in to do that
Don't have an account?
ArunKumar Kumkumath
Account Update trigger error when an account is edited
HI,
I am using a simple trigger in my application to fetch duplicate accounts based on the account name, city and street.
This trigger is working perfect while a new account is saved. If there is a match it is alerting the user and if no duplicates found, it is saved in SF.
But for edit account, it is working correct when there is a duplicate is present. If there is no duplicate account present in SF it is alerting an incorrect message followed with an error message to the user.
Below is the trigger code...
trigger AccountDuplicateTrigger on Account (before insert, before update)
{
for(Account a:Trigger.new)
{
if(Trigger.isBefore)
{
string dupItem = '';
List<Account> acc= new List<Account>();
if(Trigger.isInsert)
{
if(a.Name !='')
{
if((a.BillingStreet != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
String.escapeSingleQuotes(a.BillingStreet)+'\'';
}
if((a.BillingCity != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
String.escapeSingleQuotes(a.BillingCity)+'\'';
}
//Alerts the user if any matching record present in the system and can't allows to save it.
if(acc.isEmpty()==False) {
if(!test.isRunningTest())
{
a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');
}
}
}
}
else if(Trigger.isUpdate)
{
if(a.Name !='')
{
if((a.BillingStreet != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
String.escapeSingleQuotes(a.BillingStreet)+'\'';
}
if((a.BillingCity != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
String.escapeSingleQuotes(a.BillingCity)+'\'';
}
//Alerts the user if any matching record present in the system and can't allows to save it.
if(acc.isEmpty()==False) {
if(!test.isRunningTest())
{
a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');
}
}
}
}
}
}
}
I am using a simple trigger in my application to fetch duplicate accounts based on the account name, city and street.
This trigger is working perfect while a new account is saved. If there is a match it is alerting the user and if no duplicates found, it is saved in SF.
But for edit account, it is working correct when there is a duplicate is present. If there is no duplicate account present in SF it is alerting an incorrect message followed with an error message to the user.
- You cannot create a duplicate account here as the Name 'Test Acc1' and Phone '(000) 000-0000' already belongs to a different account.
- The record couldn’t be saved because it failed to trigger a flow. A flow trigger failed to execute the flow with version ID 301j0000000bv9s. Flow error messages: <b>An unhandled fault has occurred in this flow</b><br>An unhandled fault has occurred while processing the flow. Please contact your system administrator for more information. Contact your administrator for help
Below is the trigger code...
trigger AccountDuplicateTrigger on Account (before insert, before update)
{
for(Account a:Trigger.new)
{
if(Trigger.isBefore)
{
string dupItem = '';
List<Account> acc= new List<Account>();
if(Trigger.isInsert)
{
if(a.Name !='')
{
if((a.BillingStreet != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
String.escapeSingleQuotes(a.BillingStreet)+'\'';
}
if((a.BillingCity != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
String.escapeSingleQuotes(a.BillingCity)+'\'';
}
//Alerts the user if any matching record present in the system and can't allows to save it.
if(acc.isEmpty()==False) {
if(!test.isRunningTest())
{
a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');
}
}
}
}
else if(Trigger.isUpdate)
{
if(a.Name !='')
{
if((a.BillingStreet != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingStreet=:a.BillingStreet];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing street \''+
String.escapeSingleQuotes(a.BillingStreet)+'\'';
}
if((a.BillingCity != null) && (acc.isEmpty()))
{
acc=[select ID from account where Name=:a.Name and BillingCity=:a.BillingCity];
if(acc.isEmpty()==False)
dupItem='Name \''+ String.escapeSingleQuotes(a.Name)+'\' and Billing city \''+
String.escapeSingleQuotes(a.BillingCity)+'\'';
}
//Alerts the user if any matching record present in the system and can't allows to save it.
if(acc.isEmpty()==False) {
if(!test.isRunningTest())
{
a.adderror('You cannot create a duplicate account here as the '+dupItem+' already belongs to a different account.');
}
}
}
}
}
}
}
I have updated the code with bulkification. You are getting duplication error possible because it is going in recursion. Not sure why flow error is thrown. Please check above code and let me know if it helps.
All Answers
You written a process builder which is throughing error. Please check Your process builder.
I have updated the code with bulkification. You are getting duplication error possible because it is going in recursion. Not sure why flow error is thrown. Please check above code and let me know if it helps.