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
Mark Keckeis 34Mark Keckeis 34 

Throw an error at top of standard page from Trigger

Hi All. I am positive I am missing something simple. I would like to throw an error on a standard page - at the top of the page - when a boolean has been set on the record. This effectively locks the reord from being adited any further. Code follows:

You can see the error I would like to throw. Problem is that the error thrown looks like this:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger SalesorderTrigger caused an unexpected exception, contact your administrator: SalesorderTrigger: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors: Class.SOTriggerHelper.validateSOCanBeSubmitted: line 10, column 1

 

What am I missing?

public class myException extends Exception {}
    
	public static void validateSOCanBeSubmitted(List<Netsuite_Sales_Order__c> newTrigger, List<Netsuite_Sales_Order__c> oldTrigger, Map<Id, Netsuite_Sales_Order__c> oldMap){
													
		for(Netsuite_Sales_Order__c so :oldTrigger){
			if(so.Netsuite_SO_Entered__c == true){

					so.addError( new myException('This SO has been locked because it has already been submitted'));
					
				}
		}	
Best Answer chosen by Mark Keckeis 34
Raj VakatiRaj Vakati

When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error message is displayed in the application interface.

All Answers

Raj VakatiRaj Vakati
You need to use Trigger.new record to addError not the trigger old  .Just see the below code
 
public class myException extends Exception {}
    
	public static void validateSOCanBeSubmitted(List<Netsuite_Sales_Order__c> newTrigger,
	List<Netsuite_Sales_Order__c> oldTrigger,
	Map<Id, Netsuite_Sales_Order__c> oldMap){
													
		for(Netsuite_Sales_Order__c so :newTrigger){
			if(so.Netsuite_SO_Entered__c == true){

					so.addError( new myException('This SO has been locked because it has already been submitted'));
					
				}
		}
	}

 
Raj VakatiRaj Vakati

When used on Trigger.new in before insert and before update triggers, and on Trigger.old in before delete triggers, the error message is displayed in the application interface.
This was selected as the best answer
Mark Keckeis 34Mark Keckeis 34
Raj - thanks so much for your help! That fixed it. I missed that small detail on errors thrown from triggers. Thanks again.
Raj VakatiRaj Vakati
Thanks, Mark! Mark it as solved.