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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Set ribbon button visibility based on field value (with Javascript)

(0) ShareShare
ReportReport
Posted on by 355

Hi,

I want to set the visibility of a ribbon button by using JavaScript; the button will be visible if the state is "disabled".
I wrote the following code, but I'm not sure if it's correct:

onLoad = function (executionContext) {
        var formContext = executionContext;
        var state = formContext.getAttribute("state").getValue();
        if (state.getValue() == 1) {
            formContext.getControl("myRibbonButtonName").setDisabled(true);
        }
        else {
            formContext.getControl("myRibbonButtonName").setDisabled(false);
        }
    };

I will then add the function above to the OnLoad event handler in the Form Properties:

Conflicts.png


Does it make sense?

I have the following doubts:

- is it correct to use the getAttribute method (line 3), or should I better access the state differently?

- is it correct to add the function to the OnLoad  event of the entity form, or should I set the configuration in the Ribbon Workbench instead (and how)?

I have the same question (0)
  • Verified answer
    meelamri Profile Picture
    13,216 User Group Leader on at
    RE: Set ribbon button visibility based on field value (with Javascript)

    Hi, 

    You should use ribbonworkbench to manage the button visibilty. 

    Check:

    https://d365demystified.com/2021/01/08/ribbon-button-visibility-based-on-a-field-value-in-dynamics-365-ribbon-workbench/

    https://balugajjala.wordpress.com/2017/01/27/enabledisable-a-ribbon-button-dynamically-based-on-a-form-value/

  • Joel D Profile Picture
    355 on at
    RE: Set ribbon button visibility based on field value (with Javascript)

    Hi,

    thank you.
    I'm reading the second tutorial that you linked, and I'm going to use the custom code rule.
    On Ribbon Workbench, I created the  Enable Rule with the name of my function and set the default to true, I added it to a command (the command is also associated to a JavaScript action, I assume it doesn't create any conflict, right?).

    But how can I get the state value inside my JavaScript function?

    I wrote the following, but it didn't work, as an exception is being thrown at line 3, so that I assume I should not use the getAttribute method: what should I use instead?


    onLoad = function (executionContext) {
            var formContext = executionContext.getFormContext();
            var state = formContext.getAttribute("state").getValue();
            if (state.getValue() == 1) {
                return false;
            }
            // I want to show the button if the record is disabled
            // on ribbon workbench I set default = true
            else {
                return true;
            }
        };

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Set ribbon button visibility based on field value (with Javascript)

    Hello,

    In order to check the field's value, you should place it in the form (even in the invisible state).

    Also, there is no "state" field there is "statecode".

  • Joel D Profile Picture
    355 on at
    RE: Set ribbon button visibility based on field value (with Javascript)

    Hi,

    thank you, I wrote the following, but it's not working:

     onLoad = function (executionContext) {
            var formContext = executionContext.getFormContext();
            var state = formContext.getAttribute("statecode").getValue();
            // when the record is inactive I hide the button
            if (state == 0) {
                return false;
            }
            // when it's active I show the button
            else {
                return true;
            }
        };

    While debugging, all the instructions have been hit correctly, but the button is still visible both when the value of statecode is 1 and when it's 0.

    In the Ribbon Workbench I set the Custom Rule Deafult property to True.

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at
    RE: Set ribbon button visibility based on field value (with Javascript)

    Joel,

    Can you please provide screenshot how you registered the "Enable" rule for the ribbon button?

  • Joel D Profile Picture
    355 on at
    RE: Set ribbon button visibility based on field value (with Javascript)

    Hi,

    I fixed the bugs: I got an error because I was using the following (wrong) to get the formContext:

    var formContext = executionContext.getFormContext();

    instead than the following (correct):

    var formContext = executionContext;
    




    In addition to this, I added a Crm Parameter of type Primary Control to the "enable rule -> custom rule" to pass the context:

    enable_5F00_rule.png

    screen_5F00_ribbon.png

    Now it's working as expected.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 127

#1
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 127

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 125 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans