web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Run Javascript validation when a quote is activated

(1) ShareShare
ReportReport
Posted on by 3,297
Hi All
I know how to run a validation that check mandatory fields on a quote using a plugin. Is it possible to do the same using JS on modern command bar when a user clicks activate?
I have the same question (0)
  • Vahid Ghafarpour Profile Picture
    12,228 Super User 2026 Season 1 on at
    Yes, it is possible to run a validation for mandatory fields using JavaScript by creating a custom JavaScript function and associating it with the command bar button’s OnClick event.
     
  • Samantha73 Profile Picture
    3,297 on at
    Modern Command bar not supported?
  • Suggested answer
    Daivat Vartak (v-9davar) Profile Picture
    7,841 Moderator on at
    Hello Samantha73,
     

    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:

    1. The JavaScript function you configured will execute before the actual activation process begins.

    2. Your validation logic will check the specified fields.

    3. 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.

    4. If all validations pass, the JavaScript function will complete without preventing the default action, and the Quote will proceed to the activation stage.


    5.  

    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.

     
    If my answer was helpful, please click Like, and if it solved your problem, please mark it as verified to help other community members find more. If you have further questions, please feel free to contact me.
     
    My response was crafted with AI assistance and tailored to provide detailed and actionable guidance for your Microsoft Dynamics 365 query.
     
    Regards,
    Daivat Vartak

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 196 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 129

#3
CU11031447-0 Profile Picture

CU11031447-0 100

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans