How to Prevent or Restrict Saving Based on Field Value in Dynamics 365

How to Prevent or Restrict Saving Based on Field Value in Dynamics 365

Data validation is a critical part of maintaining data quality in Dynamics 365 CRM. Organizations often require records to meet specific business conditions before users can save them.

For example:

Dynamics 365 provides multiple ways to prevent saving records when business requirements are not satisfied.

Why Restrict Record Saving?

Restricting saves helps organizations:

Common Scenarios

Opportunity Validation

Prevent saving when estimated revenue is empty.

Customer Approval

Prevent saving if approval status is not completed.

Compliance Requirements

Require mandatory business information before submission.

Service Requests

Block save when critical fields are missing.

Method 1: JavaScript Form Validation

The most common approach is using JavaScript on the form OnSave event.

Example


function preventSave(executionContext) {

    var formContext = executionContext.getFormContext();

    var status = formContext.getAttribute("new_status").getValue();

    if (status !== "Approved") {

        var eventArgs = executionContext.getEventArgs();

        eventArgs.preventDefault();

        formContext.ui.setFormNotification(
            "Record cannot be saved until approved.",
            "ERROR",
            "approvalError"
        );
    }
}

Result:

Method 2: Business Rules

Business Rules provide a low-code alternative.

Benefits:

Example:

IF Status = Draft

THEN Show Error Message

AND Require Approval Field

Although Business Rules cannot directly stop all save operations, they can enforce required conditions before users proceed.

Method 3: Power Apps Command Logic

For custom Model-Driven Apps:

This approach provides additional control over user actions.

Example Scenario

A company requires manager approval before closing an Opportunity.

Validation Logic:

Result: Prevent Save

Error Message:

"Manager approval is required before closing this opportunity."

Best Practices

Benefits

Better Data Quality

Prevent invalid records from entering the system.

Improved Compliance

Enforce organizational policies.

Enhanced User Experience

Provide immediate feedback.

Reduced Support Issues

Eliminate incorrect data entry.

Consistent Business Processes

Ensure standard procedures are followed.

Conclusion

Preventing record saves based on field values is an effective way to maintain data integrity in Dynamics 365 CRM. Whether using JavaScript, Business Rules, or custom Power Apps logic, organizations can enforce business requirements and improve overall system reliability.