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
Emily PhillipsEmily Phillips 

addError Method prevents other records from being inserted

Hello, I'm using a trigger (before insert) and trigger handler to check for duplicates on a custom object. There can only be 1 custom object record with the Status of 'Open' per Opportunity Line Item. I've used the addError method to display an error to the user if they try to add another custom object record with the status of 'Open'. The trigger works great if I'm inserting just 1 record and my error message displays correctly. However, if I insert a list of records through the Execute Anonymous window where 1 record meets the error condition and other records are valid, none of the records are inserted. I understand the addError method stops the operation but I'm not sure how to insert the valid records while preventing the duplicate. Any help is appreciated!
 
Test code: 

public class DuplicateTriggerHandler{
    
    public static void checkForDuplicate(List<Custom_Object__c> coList){
        
        Set<Id> oliId = new Set<Id>();
        for(Custom_Object__c co : coList){
            oliId.add(co.Opportunity_Line_Item__c);
            System.debug('co.Opportunity_Line_Item__c: ' + co.Opportunity_Line_Item__c);
        }
        
        List< Custom_Object__c > coList2 = [Select Id from Custom_Object__c where Opportunity_Line_Item__c in: oliId and Status__c = 'Open'];
        System.debug(' coList2:' + coList2);
        
        for(Custom_Object__c co : coList){
            if(co.Status__c == 'Open' && coList2.size() > 0){
                co.addError('Error: You cannot have more than one Custom Object Record with the status of Open.');
            } 
        }
    }
}

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Emily,

Are you inserting custom object records for different Opportunity_Line_Item__c records?

If you try to insering the same custom object records for same Opportunity_Line_Item__c record then you will get the error.

Try to modify your code like below.
public class DuplicateTriggerHandler{
    
    public static void checkForDuplicate(List<Custom_Object__c> coList){
        
        Set<Id> oliId = new Set<Id>();
        for(Custom_Object__c co : coList){
            oliId.add(co.Opportunity_Line_Item__c);
            System.debug('co.Opportunity_Line_Item__c: ' + co.Opportunity_Line_Item__c);
        }
		
		Map<Id,Custom_Object__c> mapOLIidwithvalues = new Map<Id,Custom_Object__c>();
        for(Custom_Object__c co: [Select Id,Opportunity_Line_Item__c from Custom_Object__c where Opportunity_Line_Item__c in: oliId and Status__c = 'Open']){
		mapOLIidwithvalues.put(co.Opportunity_Line_Item__c,co);
		}	
        
        for(Custom_Object__c co : coList){
            if(co.Status__c == 'Open' && mapOLIidwithvalues.containskey(co.Opportunity_Line_Item__c)){
                co.addError('Error: You cannot have more than one Custom Object Record with the status of Open.');
            } 
        }
  }
}

If this helps, Please mark it as best answer.

Thanks!!
 
Emily PhillipsEmily Phillips
Hi Ankaiah, as a test I'm trying to insert 3 Custom Object Records. I've already inserted 'Open' Custom Object Records for both Opportunity Line Items to test the validation. I'm getting the following error upon insert: 
DEBUG | The following exception has occurred: Insert failed. First exception on row 2; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error: You cannot have more than one Custom Object Record with the status of Open.: []
Below is an example of how I'm inserting the records through Execute Anonymous: 
List<Custom_Object_c> coList = new List<Custom_Object__c>();

Custom_Object_c co0 = new Custom_Object__c();
co0.Opportunity_Line_Item__c = 'Test OLI 1';
co0.Status__c = 'Completed';

Custom_Object_c co1 = new Custom_Object__c();
co1.Opportunity_Line_Item__c = 'Test OLI 2';
co1.Status__c = 'Completed';

Custom_Object_c co2 = new Custom_Object__c();
co2.Opportunity_Line_Item__c = 'Test OLI 2';
co2.Status__c = 'Open';

coList.add(co0);
coList.add(co1);
coList.add(co2);

insert coList;
The records with the Status__c of 'Completed' should be inserted. Thank you!