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

Announcements

News and Announcements icon
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 365

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,218 User Group Leader on at
  • Joel D Profile Picture
    365 on at

    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

    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
    365 on at

    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

    Joel,

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

  • Joel D Profile Picture
    365 on at

    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 94 Super User 2026 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 68 Most Valuable Professional

#3
ParthPatel249 Profile Picture

ParthPatel249 62

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans