Yes, it is absolutely possible to run JavaScript validation on the modern command bar when a user clicks the "Activate" button on a Quote in Dynamics 365. This provides a more immediate and user-friendly feedback mechanism compared to a server-side plugin, which would only trigger after the activation attempt.
Here's how you can achieve this:
1. Identify the "Activate" Command:
- In the modern command bar designer for the Quote entity, locate the "Activate" button. It's usually an out-of-the-box command.
2. Create Your JavaScript Web Resource:
- Write a JavaScript function that contains your validation logic. This function should:
- Get the
formContext of the Quote record.
- Use
formContext.getAttribute("your_field_schema_name").getValue() to retrieve the values of the fields you want to validate.
- Implement your validation rules (e.g., checking if mandatory fields are filled, comparing values, etc.).
- If validation fails:
- Use
formContext.ui.setFormNotification("Your validation error message.", "ERROR", "validation_error_id"); to display an error message at the top of the form.
- Use
executionContext.getEventArgs().preventDefault(); to prevent the "Activate" action from proceeding.
- If validation passes, the function should do nothing, allowing the "Activate" action to continue.
Example JavaScript Code:
function validateQuoteOnActivate(executionContext) {
var formContext = executionContext.getFormContext();
var isAllValid = true;
var errorMessage = "";
// Check if the 'description' field is mandatory
var description = formContext.getAttribute("description").getValue();
if (!description || description.trim() === "") {
errorMessage += "Description is a mandatory field before activation.\n";
isAllValid = false;
formContext.getControl("description").setFocus(); // Optionally set focus to the invalid field
}
// Check if the 'customerid' (potential customer) lookup is filled
var customerId = formContext.getAttribute("customerid").getValue();
if (!customerId || customerId.length === 0) {
errorMessage += "Potential Customer is a mandatory field before activation.\n";
isAllValid = false;
if (formContext.getControl("customerid")) {
formContext.getControl("customerid").setFocus();
}
}
// Add more validation checks for other fields as needed
if (!isAllValid) {
formContext.ui.setFormNotification(errorMessage, "ERROR", "activate_validation_error");
executionContext.getEventArgs().preventDefault(); // Prevent activation
} else {
// Clear any previous error notification if validation passes
formContext.ui.clearFormNotification("activate_validation_error");
}
}
3. Create a JavaScript Web Resource in Dynamics 365:
- Go to Settings > Customization > Customize the System.
- In the Solution Explorer, navigate to Web Resources.
- Click New.
- Enter a Name (e.g.,
quote_activate_validation.js).
- Set the Type to Script (JScript).
- Click Text Editor and paste your JavaScript code into the editor.
- Click Save and then Publish the web resource.
4. Configure the Command for the "Activate" Button:
- Go back to Settings > Customization > Customize the System.
- In the Solution Explorer, expand Entities and select the Quote entity.
- Click on Command bar.
- In the command bar designer, locate the "Activate" button. You might need to select the "Main Grid" or "Main Form" depending on where you want this validation to run. It's generally best to apply it to the "Main Form" Activate button.
- Select the "Activate" command associated with the button.
- In the right-hand properties pane, under the Actions section, click Add action.
- Choose JavaScript action.
- In the Library dropdown, select the web resource you created (
quote_activate_validation.js).
- In the Function Name field, enter the name of your JavaScript function (
validateQuoteOnActivate).
- Under the Parameters section, click Add parameter.
- Select Primary Control. This will pass the
formContext to your JavaScript function.
- Click Save and then Publish All Customizations.
How it Works:
When a user clicks the "Activate" button on a Quote form:
- The JavaScript function you configured will execute before the actual activation process begins.
- Your validation logic will check the specified fields.
- If any validation fails, an error message will be displayed to the user at the top of the form, and
preventDefault() will stop the activation.
- If all validations pass, the JavaScript function will complete without preventing the default action, and the Quote will proceed to the activation stage.
Advantages of using JavaScript on the Command Bar:
- Immediate Feedback: Users receive validation errors directly on the form before the activation attempt is processed server-side.
- Improved User Experience: Prevents unnecessary server-side processing and potential errors if mandatory data is missing.
- Targeted Validation: You can implement specific validation rules relevant to the activation process.
Important Considerations:
- Maintainability: Ensure your JavaScript code is well-commented and easy to maintain.
- Performance: Keep your validation logic efficient to avoid delaying the button click response.
- Server-Side Validation (Still Recommended): While client-side JavaScript provides a good user experience, it's still crucial to have server-side validation (using plugins or workflows) as a backup. Client-side validation can be bypassed in certain scenarios (e.g., using the Web API directly). Having server-side validation ensures data integrity regardless of how the activation is attempted.
- Error Handling: Provide clear and informative error messages to the user.
- Testing: Thoroughly test your JavaScript validation in various scenarios to ensure it works as expected.
By implementing this approach, you can effectively run JavaScript validation when a user clicks the "Activate" button on a Quote, providing real-time feedback and preventing activation if critical data is missing.