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:
- Opportunity Amount must be greater than zero.
- Customer Status must be approved.
- Mandatory fields must contain values.
- Certain combinations of fields must be restricted.
Dynamics 365 provides multiple ways to prevent saving records when business requirements are not satisfied.
Why Restrict Record Saving?
Restricting saves helps organizations:
- Improve data quality.
- Enforce business policies.
- Reduce reporting errors.
- Prevent invalid transactions.
- Improve user accountability.
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:
- Save operation is cancelled.
- User receives an error message.
- Data remains unchanged.
Method 2: Business Rules
Business Rules provide a low-code alternative.
Benefits:
- No JavaScript required.
- Easy maintenance.
- Supported by Microsoft.
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:
- Disable Save buttons.
- Control ribbon commands.
- Apply role-based restrictions.
This approach provides additional control over user actions.
Example Scenario
A company requires manager approval before closing an Opportunity.
Validation Logic:
- Status = Closed
- Approval = No
Result: Prevent Save
Error Message:
"Manager approval is required before closing this opportunity."
Best Practices
- Use Business Rules for simple validations.
- Use JavaScript for complex business logic.
- Provide clear error messages.
- Avoid excessive client-side scripting.
- Test validations in all supported browsers.
- Validate data server-side when necessary.
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.