Skip to main content
1 of 2

SObject row does not allow errors when checking for duplicate lookup field value

I am trying to check duplicate lookup field values in a custom object named ABC__c. The lookup field is named RelatedUser__c and references the standard User object in Salesforce. Aside from this, there is another lookup field named Alphabet__c which references a custom object named Alphabet__c.

The rule is that there should be an error when RelatedUser__c already assigned to same Alphabet__c record is inputted to new ABC__c record.

I created an apex class and trigger to implement said requirement, but I am getting the following error:

System.FinalException: SObject row does not allow errors

I am kind of stuck right now and don't know what went wrong. How can I fix this?

ABCController

public with sharing class ABCController {

public static void duplicateMethod() {

    List<Alphabet__c> alphabetList = new List<Alphabet__c>();
    List<ABC__c> abcList = new List<ABC__c>();
    
    alphabetList = [SELECT Id, Name FROM Alphabet__c];
    
    abcList = [SELECT Id, Name, Alphabet__c, RelatedUser__c FROM ABC__c 
                    WHERE Alphabet__c IN :alphabetList];
                    
    for(ABC__c newABC : abcList) {
         for(ABC__c existingABC : abcList) {
             if(newABC.RelatedUser__c == existingABC.RelatedUser__c)  {
                 newABC.addError('related user is already assigned to same alphabet');      
             }
         }
    }    

}

}

ABCTrigger

trigger ABCTrigger on ABC__c (before insert, before update) {

    ABCController.duplicateMethod();  

}

I am quite grasping out the basic in Salesforce development but I hope I have clearly explained my issue here. Please bear with me.