When developing K2 SmartForms, most developers tend not to implement user friendly messages. In this blog, we will go through step-by-step how to implement error handling that results in a user-friendly K2 error message being displayed to the users when an error is raised. The method for doing this in K2 SmartForms is similar to how we do it for conventional .NET applications.
Let’s see how to implement this.
The screenshot below shows an example implementation with each of the steps to follow to implement error handling in K2 SmartForms that displays user-friendly messages upon the “If an error occurred” SmartForm condition:
First, use the “If an error occurred” condition to catch the error and then transfer the error message to the form/view parameter.
We must use the action “continue to next execution” otherwise the rule will stop executing after transferring the error message to the parameter.
Now we have an error message. Based on the message, we need to add multiple “if-else” conditions to show the custom error message.
For example, if there is an unique key violation, then the default error message will be as below:
Violation of UNIQUE KEY constraint ‘IX_employeename’. Cannot insert duplicate key in object ‘dbo.Employees’. The duplicate key value is (John).The statement has been terminated.
To define a user-friendly error message for this error, check if the error message parameter contains “Violation of UNIQUE KEY constraint” and a rule to “Show a message to the user”. An example is shown below:
If none of the conditions match, simply display the original message to the user.
Finally, remember to clear the error message parameter.