Implementing Column Validation with Form Notification in Dynamics 365
Introduction:
In Dynamics 365, ensuring data integrity and enforcing business rules during data entry are crucial. This article demonstrates a scenario where we validate column values on a form, utilizing JavaScript to enhance user experience and data accuracy. The example involves checking if a certain condition is met and, if not, displaying a notification to the user.
Scenario:
We have a custom table, CustomTable
, with two fields: CustomEnforceField
(a binary field) and CustomTotalField
(a numeric field). The business requirement dictates that if CustomEnforceField
is set to a specific value (let's say true
), CustomTotalField
must be greater than 0. If this condition isn't met, the user should receive an immediate notification when attempting to save the form.
Implementation:
The implementation is done via JavaScript, attached to the form's onSave event. The script comprises two functions: onSave
and validateTotalField
.
Code:
javascriptfunction onSave(executionContext){validateTotalField(executionContext);}function validateTotalField(executionContext){var formContext = executionContext.getFormContext();var enforceField = formContext.getAttribute("CustomEnforceField").getValue();var totalField = formContext.getAttribute("CustomTotalField").getValue();if (enforceField == true && totalField <= 0){formContext.ui.setFormNotification("Total must be greater than 0 when Enforce is set to Yes.", "ERROR", "totalFieldError");executionContext.getEventArgs().preventDefault();}else{formContext.ui.clearFormNotification("totalFieldError");}}Form notification sample:
Explanation:
- onSave Function: This function is triggered when the form is saved. It calls the
validateTotalField
function, passing theexecutionContext
. - validateTotalField Function: It retrieves the context of the form and then the values of
CustomEnforceField
andCustomTotalField
. IfCustomEnforceField
is true andCustomTotalField
is 0 or less, a form notification is displayed, and the save operation is prevented. If the condition is satisfied, any existing notification related to this validation is cleared.
No comments:
Post a Comment