+ Start a Discussion
prathibha krprathibha kr 

How to add try catch error message using trigger

Is it possible to show the try catch error message from trigger to record page. So that proper error message will be visible for user
Best Answer chosen by prathibha kr
DevADSDevADS
Hi Prathibha,

If your trigger is invoking from Standard record detail page then you use "sObject.addError(<Message>)" method to show an error.

Refer to following code snippet - 
for(Account accItr : Trigger.new){
	if(accItr.contactCounter__c != null && accItr.contactCounter__c >= 10 ){
		accItr.addError("You can not have more than 10 associated contacts");
	} 
}

This error would be specific to sObject record however if you want to use try catch block then you can throw a custom exception and attach it to the specific sobject record.

Do post here if you have any further questions.

Happy Coding!!