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
angusgrantangusgrant 

Cant work out how to add an error message to be displayed to users on the execution of a trigger.

Hi Every time I run this trigger. I get sent a Apex exception email with the following how do I add an error ?

 

System.Exception: SObject row does not allow errors  

 

trigger EventDuplicateTigger on SBS_Event__c (before insert) {

Set<string> eventnameSet = new Set<string>();

for (SBS_Event__c e : trigger.new){
eventnameSet.add(e.Name);
}

SBS_Event__c[] events = [select id from SBS_Event__c where Name in : eventnameSet];
if (events.size() > 0) {
for(SBS_Event__c e:events){
e.Name.addError('Event cannot be created - Event already exists');
}
}
}

 


Best Answer chosen by Admin (Salesforce Developers) 
hisrinuhisrinu

Hi,

 

You can display error messages only in the case of New context variable. So as long as your addError is inside the first loop it will work fine (because you are looping through the Trigger.New). Hope this helps.

All Answers

hisrinuhisrinu

Hi,

 

You can display error messages only in the case of New context variable. So as long as your addError is inside the first loop it will work fine (because you are looping through the Trigger.New). Hope this helps.

This was selected as the best answer
angusgrantangusgrant

Thank you for your help the answer was obvious I just could not see it. thanks again for your help.

 

trigger EventDuplicateTigger on SBS_Event__c (before insert) { Set<string> eventnameSet = new Set<string>(); for (SBS_Event__c e : trigger.new){ eventnameSet.add(e.Name); } SBS_Event__c[] events = [select id from SBS_Event__c where Name in : eventnameSet]; if (events.size() > 0) { for(SBS_Event__c e: trigger.new){ e.name.addError('Event cannot be created - Event already exists'); } } }