function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hermann Ouré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
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
 
Hermann OuréHermann Ouré
ps:
Whilst updating my code line 17
for(Account_Relation__c relAcc : Trigger.New) {
}

with
for(Account_Relation__c relAcc : arList) {
}

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: []

 
Hermann OuréHermann Ouré
Hi CharuDutt,
Please look at the first post