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
sai durga reddy medapati 8sai durga reddy medapati 8 

How to throw error with sales force trigger under a field or on a top of page?

Hi I am new to triggers and I would like to know how to throw normal errors under a field or on the top of the page.
Also please provide with a sample example with explanation.
Best Answer chosen by sai durga reddy medapati 8
Kanav TechfineryKanav Techfinery

Hi Sai Durga,

" throw normal errors under a field or on the top of the page" -> As mentioned by Anutej above, you may use <record>.addError. It can be used in the following ways:

To show the error on a specific field: 

 

Trigger.new[0].myField__c.addError('bad');
To show the error on top of the page:

Trigger.new[0].addError('bad');


You can reference this article for more information:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError

 

Please mark this as the 'Best answer' if you find this information useful. 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Sai Durga,

There is an implementation in below link that has a similar implementation I am posting the code as well for quick reference:

Link: https://developer.salesforce.com/forums/?id=9060G0000005cjKQAQ
 
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'));
					
				}
		}
	}

I hope you find this useful.

Regards,
Anutej
Kanav TechfineryKanav Techfinery

Hi Sai Durga,

" throw normal errors under a field or on the top of the page" -> As mentioned by Anutej above, you may use <record>.addError. It can be used in the following ways:

To show the error on a specific field: 

 

Trigger.new[0].myField__c.addError('bad');
To show the error on top of the page:

Trigger.new[0].addError('bad');


You can reference this article for more information:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_sobject.htm#apex_System_SObject_addError

 

Please mark this as the 'Best answer' if you find this information useful. 

This was selected as the best answer
sai durga reddy medapati 8sai durga reddy medapati 8
@kanav can you please explain why we use Trigger.new[0]. the remaining part is understandable.
ANUTEJANUTEJ (Salesforce Developers) 
I think as trigger.new is a list of new records and to mention the error on a specific record without moving through the loop you can use the above solution that was selected as the best answer.
Kanav TechfineryKanav Techfinery

Hi Sai,

'Trigger.New[0]'  is accessing the first record in the list 'Trigger.New'. This can be translated in English as:

Get me the first record (index 0) in the List of triggering records. 

Bear in mind that .addError can only be used on an instance of 'Trigger.New'.  
Hope that helps!