Skip to main content

Notifications

Announcements

No record found.

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?

Posted on by 355

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?

  • Suggested answer
    prt33k Profile Picture
    prt33k 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans