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
Edgar KAMDEMEdgar KAMDEM 

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: Trigger.new){
          
              livreNew.add(p.Livre__c);
              contactNew.add(p.Contact__c);
        }

       
        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);
        
            if(!relatedPrets.isEmpty()){
               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) 
Hi,

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


Also refer below links for similar discussion.

https://success.salesforce.com/issues_view?id=a1p30000000Sz5RAAS
 
https://developer.salesforce.com/forums/?id=906F00000008wsTIAQ
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
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

p.Field_APINAME__c.addError();

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
www.varasi.com