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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

set visibility of a button with JS, how should I pass the context and refer to the button?

(0) ShareShare
ReportReport
Posted on by 363

I created the button on Ribbon Workbench and associated it to a command, which is related to a JS function (emtpy, for now).

I need to implement this JS function to set the visibility of a button.

It's not clear to me how should I pass the execution context to the function and how should I refer to the button (should I use its logical name as if it was a field?)

I saw the following example related to a script which was associated to a form:

 formContext.getControl("logical_name_of_the_button?").setVisible(false);

...but in that case the formContext was retrieved through the execution context, which is a parameter passed by default to the function while setting the web resource from crm configuration (by selecting "pass the execution context as first parameter"), is it correct?

If it's correct, how should I pass the context in this case instead?

I have the same question (0)
  • Suggested answer
    prt33k Profile Picture
    6,907 on at
    RE: set visibility of a button with JS, how should I pass the context and refer to the button?

    Hi,

    You need to create a enable rule and pass the primarycontrol as CRMParameter which is nothing but the form context. You will not get executioncontext - just directly the formcontext.

    Below is a sample :

    pastedimage1611361428961v1.png

    Code can be something like this:

     function RibbonRule(formContext) 
     {
            var recordId = formContext.data.entity.getId().replace("{", "").replace("}", "");
            var globalContext = Xrm.Utility.getGlobalContext();
            var serverUrl = globalContext.getClientUrl();
            var url = Xrm.Page.context.getClientUrl()   "/api/data/v9.1/ps_configurationsettings?$filter=ps_key eq 'key'";
            var reqCS = new XMLHttpRequest();
    
            reqCS.open("GET", url, false);
            reqCS.setRequestHeader("Accept", "application/json");
            reqCS.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            reqCS.send();
    
            if (reqCS.readyState == 4) {
                if (reqCS.status == 200) {
                    var data = JSON.parse(reqCS.response);
                    if (data != null && data.value.length > 0) {
                         return false;
                    }
                }
                else {
                    return true;
                }
            }
          return true;
    }

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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans