You need to sign in to do that
Don't have an account?
Travis Malle
Throw a user friendly error message in apex
I have a class that is called by a before trigger. The class is designed to loop through records and evaluate who it belongs to and the datetime. there are a few things happen with this logic that are outside of this post. I now have a need to present a user friendly error message if there are more than one record with the same owner and same datetime. I am able to successfully throw the error and prevent a save (exactly what I want). However, I would like a more user friendly error message. Here is a snip of how my error is triggered and the resulting message. Any way to clean this up??? Something more akin to validation message
else if(officer == prevOfficer && StartDate == prevDate){ // same officer and exact same time (throw error) if(initialTime == prevDateTime){ throw new CustomException('Cannot have two events start at the same time'); }
When I use the addError method, i get an even more cryptic message. See image below.
Below is one possible solution with Unique field. Which displays user friendly message(However in lightening (https://success.salesforce.com/ideaView?id=0873A000000LmkwQAC), it still having a problem).
Step1: Create a text field with Unique Type
Step2: Create a workflow rule load this field with following forumula (OwnerId + TEXT( ActivityDateTime )) and click the check box Re-evaluate Workflow Rules after Field Change.
Step3: Activate the Workflow rule.
If user creates an event from onwords with same start date time they will see an error here