Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

(0) ShareShare
ReportReport
Posted on by 270
Hello, I am trying to develop a custom JS. The code runs onChange a field that will show or hide a ribbon button based on the option it's selected.
I am getting error on option1. Thanks.


function refreshRibbonOnChange() { Xrm.Page.ui.refreshRibbon(); } function enableCustomRule() { if (Xrm.Page.data.entity.attributes.get("new_category") != null) { var category = Xrm.Page.data.entity.attributes.get("new_category").getValue()[0].id; var req = new XMLHttpRequest(); var url = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/CategorySet(guid'" + category + "')?$select=option1"; req.open("GET", encodeURI(url), false); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.send(); var data = JSON.parse(req.responseText).d; if (data.option1 == null || data.option1 == false) { return true; } return false; } }

*This post is locked for comments

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    I have it working for all of the lookup records. I am calling the refershRibbon onChange so that show the buttons for all of the lookup records

    But I need to filter it for the one's I need. I know I need to use fetchXml but not so clear on how I need to build the fetch.

    function showRibbonButton() {

        var categoryType = Xrm.Page.getAttribute("new_categorytype").getValue()[0].id;
        if (categoryType != null) {
            return true;
    
        } else 
            return false;
    }
    
    function refreshRibbon(){
        Xrm.Page.ui.refreshRibbon();
    }


    I built the following fetch....

    function addLookupFilter() {
    var category = Xrm.Page.getAttribute("new_categorytype").getValue();
    if (category != null) {
    fetchXml = "<filter type='and'><condition attribute='new_categorytype' operator='eq' value='" + category + "' /></filter>"; 
    Xrm.Page.getControl("new_categorytype").addCustomFilter(fetchXml);
    }
    }


  • Nithya Gopinath Profile Picture
    Nithya Gopinath 17,076 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Do you mean option1 is a field in Category entity?

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Thank you Alex! option1 is a record which is a category type. I have field called Category Type which is the lookup field.

  • ashlega Profile Picture
    ashlega 34,477 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Hi,

     you might want to try the REST builder then:

    github.com/.../CRMRESTBuilder

     After the "$select" (in the url), you should list all the attributes. I don't think option1 is the name of the attribute.. 

     Here is an example:

    https://msdn.microsoft.com/en-us/library/gg309461(v=crm.7).aspx#BKMK_select

     That same attribute name should be used later in place of data.option1

     Is there an attribute of the the new_category entity that you need to retrieve? You should be using that field name in place of option1.

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Ethan, no it's not in BPF it's in a form. The form has a field which is a lookup. The lookup field has a list so based on the value selected from the lookup field, I want to show/hide a ribbon button.

  • MituCRMing Profile Picture
    MituCRMing 270 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Hi Alex, the schema name is new_category. option1, option2...e.t.c are the values of new_category

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Bit more info needed perhaps? 

    If I understand this right, you're wanting a field on the business process flow for your entity, to be shown or hidden, based on another field which I'm assuming is an option set? 

    If that's all correct, can you please give the option set values? I'm also struggling to understand why you're using JSON here.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,477 on at
    RE: Dynamic CRM Hide/Show ribbon button by OnChange of a lookup field

    Hi,

     what is the schema name of that field? There should be some prefix.. something like new_option1 (in both the url you are using and in the code that follows)

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans