" MicromOne: Preventing Invalid Operations with Form Save Validation in Dynamics 365

Pagine

Preventing Invalid Operations with Form Save Validation in Dynamics 365

When developing customizations for Dynamics 365, it's common to trigger business logic from a command button or a custom JavaScript action. However, there's one important aspect that developers often overlook: ensuring that the current form contains valid and saved data before executing the logic.

In this article, I'll show a simple but effective approach to validate the form by forcing a save operation before continuing with the custom process.

The Problem

Imagine a custom button that creates a related record, such as an Article, based on the current Request.

If the user has modified the form but some required fields are missing or a business rule prevents the record from being saved, your custom logic may still execute, resulting in inconsistent or incomplete data.

Instead, it's a good practice to validate the form by attempting to save it first.

The Solution

The following code attempts to save the current form before executing the remaining business logic.

try {
    await formContext.data.save();
} catch (saveError) {
    Xrm.Navigation.openAlertDialog({
        title: "Warning!",
        text: "The request data is not valid. Before creating the article, verify the request information."
    });
    return;
}

How It Works

The implementation is straightforward:

  1. formContext.data.save() attempts to save the current record.

  2. If the save succeeds, the script continues with the remaining logic.

  3. If the save fails because of validation errors, required fields, Business Rules, or server-side validation, an exception is thrown.

  4. The catch block displays a friendly message to the user and stops the execution with a return.

This approach guarantees that the subsequent business logic only runs on valid and successfully saved data.

Why This Is Better

Compared to manually checking every required field, forcing a save provides several advantages:

  • It respects Dynamics 365 native validation.

  • It automatically handles Business Rules.

  • It validates server-side plugins and synchronous processes.

  • It reduces the amount of custom validation code.

  • It prevents downstream processes from working with invalid data.

Best Practices

When using this pattern, keep a few recommendations in mind:

  • Use await so the save operation completes before continuing.

  • Catch the exception to provide a clear and user-friendly error message.

  • Exit the function immediately after the failed save.

  • Let Dynamics 365 handle validation instead of duplicating business logic in JavaScript.

A simple try...catch around formContext.data.save() can significantly improve the reliability of your Dynamics 365 customizations.

Rather than assuming the current form is valid, let the platform perform its built-in validation first. This small change helps prevent inconsistent data, avoids unexpected errors later in the process, and provides a much better user experience.

Sometimes, the simplest solutions are also the most effective.