You need to sign in to do that
Don't have an account?
Hermann Ouré
Trigger to prevent duplicate records
Hello,
I wrote a trigger to prevent duplicate records but now I am not even able to create new records.
Anytime I try to create a new record, I have the following error
[AccountRelationsTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A relation already exists between the 2 Accounts: [Related_Account__c] Class.AccountRelationHandler.createAccountRelation: line 20, column 1 Trigger.AccountRelationsTrigger: line 6, column 1]
here is the trigger
here is the class:
I wrote a trigger to prevent duplicate records but now I am not even able to create new records.
Anytime I try to create a new record, I have the following error
[AccountRelationsTrigger: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, A relation already exists between the 2 Accounts: [Related_Account__c] Class.AccountRelationHandler.createAccountRelation: line 20, column 1 Trigger.AccountRelationsTrigger: line 6, column 1]
here is the trigger
trigger AccountRelationsTrigger on Account_Relation__c (before insert, after insert) { if(Trigger.isAfter) { if(!AccountRelationHandler.isRecursion) AccountRelationHandler.createAccountRelation(Trigger.New); } if(Trigger.isBefore && Trigger.isInsert) { Set<Id> relatedAccIds = new Set<Id>(); for(Account_Relation__c ar : Trigger.New) { relatedAccIds.add(ar.Related_Account__c); } List<Account_Relation__c> arList = [SELECT Related_Account__c FROM Account_Relation__c WHERE Related_Account__c = :relatedAccIds]; for(Account_Relation__c relAcc : Trigger.New) { if(arList.size() > 0) { relAcc.Related_Account__c.addError('A relation already exists between the 2 Accounts'); } } } }
here is the class:
public class AccountRelationHandler { public static Boolean isRecursion = false; public static void createAccountRelation(List<Account_Relation__c> accRelationList) { // We are creating the inverse records, so return now if(isRecursion) { return; } isRecursion = true; Map<Id, Account> accRelatedToAccount = new Map<Id, Account>( [SELECT Id, Name, (SELECT Account__c, Related_Account__c FROM Account_Relations__r) FROM Account]); Account_Relation__c[] newRelations = new Account_Relation__c[0]; for(Account_Relation__c acc : accRelationList) { newRelations.add(new Account_Relation__c(Name=acc.Name, Account__c=acc.Related_Account__c, Related_Account__c=acc.Account__c)); } insert newRelations; isRecursion = false; } }Thanks for any help
Whilst updating my code line 17
with
I have another error
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountRelationsTrigger: execution of BeforeInsert caused by: System.FinalException: SObject row does not allow errors Trigger.AccountRelationsTrigger: line 19, column 1: []
Please look at the first post