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

System.Exception: SObject row does not allow errors - trying to prevent dupes

I created a trigger that work very except that I got this error when I try to use addError.
Here is my trigger: 
trigger MyTestTrigger on Pret__c (before insert) {
       Set<Id> livreNew = new Set<Id>{};
       Set<Id> contactNew = new Set<Id>{};
        for(Pret__c p:{

        List<Pret__c> relatedPrets = [SELECT Id, Contact__c, Livre__c FROM Pret__c
        WHERE Livre__c IN :livreNew AND Contact__c IN :contactNew];
        system.debug('relatedPrets '+relatedPrets);
               for(Pret__c p: relatedPrets){
                system.debug('Trigger p.Id '+p.Id);
                Trigger.oldMap.get(p.Id).addError('Error - dupe item');
Pret__c is a junction object between Livre__c and Contact__c
Where I'm wrong ? please help :(
SandhyaSandhya (Salesforce Developers) 

You can use if(custom condition) { 
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, '*FAILED*')); 

Also refer below links for similar discussion.
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Jainam ContractorJainam Contractor
Hi Edgar,

I hope you want to AddError on the Pret__c object in case of duplicate... Also you are using before insert which doesn't support Trigger.OldMap which are using for Error Display.

 In such scenario, I would recomment you to use the below line for Error Display.
p.addError('Error - dupe item');

The above line will display error on the Top of the record. If you want to display error on some particular field then use


Please try this and let me know if it works.

Mark this as the solution if it solved your purpose or you need anymore assistance.

Thanks & Regards,
Jainam Contractor,
Salesforce Consultant,
Varasi LLC