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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Advanced Find button hide based on security role using javascript in crm 365

(0) ShareShare
ReportReport
Posted on by 2,667

Hi All,

I need to show button Results,Save & Save as button from Advanced find for system admin role only.

Is it possible using javascript in crm any reference link ?

Thanks,

Jharana

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Create new solution and add Application Ribbon component to your solution.

    Now open this solution from ribbon workbench tool to customize buttons.

    www.crmug.com/.../viewthread

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Bipin,

    Thanks for ur response.

    I am using below method:

    /// hide results button for other roles except system admin, system customizer & Tech Admin

    Deal_Retail_Ribbon.hideResults = function(PrimaryControl) {

       "use strict";

       try {

           if (PrimaryControl !== null && PrimaryControl !== undefined) {

               var FormContext = PrimaryControl;

               //var leadId = FormContext.data.entity.getId();

               return new Promise(function(resolve, reject) {

                   Xrm.WebApi.retrieveMultipleRecords("role", "?$select=roleid&$filter=name eq 'Tech Admin'").then(

                       function success(result) {

                           var userSettings = Xrm.Utility.getGlobalContext().userSettings;

                           var securityRoles = userSettings.securityRoles;

                           var isUserAdmin = false;

                           for (var i = 0; i < result.entities.length; i++) {

                               var roleId = result.entities[i].roleid;

                               if (securityRoles.indexOf(roleId) !== -1) {

                                   isUserAdmin = true;

                                   break;

                               }

                           }

                           // return true or false

                           resolve(isUserAdmin);

                       },

                       function(error) {

                           reject(error.message);

                           console.log(error.message);

                       }

                   );

               });

           }

       } catch (e) {

           common_Retail.openAlertDialog("Ok", e.message);

       }

    };

    pastedimage1672836877645v1.png

    calling above method by doing customize command on results button & using custom javascript action is it correct or aything i need to call ?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Steps you followed are correct.

    Does it work?

    Add debugger in your code, put alert also to troubleshoot.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You forgot to select library.

    Also why you are adding under commands?

    It should go under Enable Rule/Display Rule... And associate your new rule to Command

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Bipin,

    Thanks for ur response.

    I have selected the web resource & added now enable rule by adding custom rule called my method. but i am unable to debug the web resource file is not showing while debugging


    pastedimage1672907749364v1.png
    pastedimage1672907860579v2.png
    But if i am clicking on button to check whether its associated with command or not its not displaying anything in r8 hand side what could be the issue
    pastedimage1672907940050v3.png

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    You have to right click on button and say customize command

    Then you need to associate new rule to command

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    But strangely its not showing any properties on Results button

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi Bipin,

    Below code tried but its returning value true/false as per condition but not working.

    /// hide results button for other roles except system admin,system customizer & Tech Admin

    Deal_Retail_Ribbon.hideResults = async function(PrimaryControl) {

       "use strict";

       try {

           debugger;

           var isValidate = false;        

           var roleName = await Deal_Retail_Ribbon.getRoleName();      

           var fetchXMLCondition = "";

           var userSettings = Xrm.Utility.getGlobalContext().userSettings;

           var loggedInUserSecurityRolesArray = userSettings.securityRoles;                

           if (loggedInUserSecurityRolesArray.length > 0) {

               var i;

               for (i = 0; i < loggedInUserSecurityRolesArray.length; i++) {

                   fetchXMLCondition += "<condition attribute='roleid' operator='eq' value='" + loggedInUserSecurityRolesArray[i] + "'/>";

               }

               var fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +

                   "<entity name='role'>" +

                   "<attribute name='roleid' />" +

                   "<attribute name='name' />" +

                   "<order attribute='name' descending='false' />" +

                   "<filter type='or'>" + fetchXMLCondition +

                   "</filter>" +

                   "</entity>" +

                   "</fetch>";

               fetchXML = "?fetchXml=" + encodeURIComponent(fetchXML);

               isValidate = await Deal_Retail_Ribbon.checkMatchRole(fetchXML,roleName);

               Xrm.Page.ui.refreshRibbon();

    }

    return isValidate;

       } catch (error) {

    Xrm.Navigation.openAlertDialog("Ok", error.message);

           //common_Retail.openAlertDialog("Ok", error.message);

       }

    };

    ///Get Logged in Role Names

    Deal_Retail_Ribbon.getRoleName = function() {

       "use strict";

       try {

           var roleName = new Array();

           return new Promise(resolve => {

               Xrm.WebApi.online.retrieveMultipleRecords("new_configuration", "?$select=new_name,new_value&$filter=new_name eq 'GetRoleName'").then(

                   function success(results) {

                       results.entities.forEach(function(result) {

                           var new_name = result["new_name"];

                           var new_value = result["new_value"];

                           roleName = new_value.split(',');

                       });

                       //console.log(roleName);

                       setTimeout(() => resolve(roleName, 1000))

                   },

                   function(error) {

    Xrm.Navigation.openAlertDialog("Ok", error.message);

                       //common_Retail.openAlertDialog("Ok", error.message);

                   }

               );

           });

           //return roleName;

       } catch (error) {

           Xrm.Navigation.openAlertDialog("Ok", error.message);

       }

    };

    ///check role matching or not

    Deal_Retail_Ribbon.checkMatchRole = function(fetchXML,roleName) {

       "use strict";

       try {      

        var counter = 0;

           return new Promise(resolve => {

                Xrm.WebApi.retrieveMultipleRecords("role", fetchXML).then(

                   function success(result) {

                       if (result !== undefined && result !== null && result.entities.length > 0) {

                           result.entities.forEach(function(itemName) {

                               if (roleName.includes(itemName.name)) {

                                   //flag = true;

    counter++;

                                   //resolve(true);

                                   setTimeout(() => resolve(true, 1000));

                               }

                              else {

          //resolve(false);

      if(counter === 0){

      setTimeout(() => resolve(false, 1000));

      }

                              }

                           });

                       }

                   },

                   function(error) {

    Xrm.Navigation.openAlertDialog("Ok", error.message);

                       //common_Retail.openAlertDialog("Ok", error.message);

                   }

               );

    //if(counter === 0){

    //resolve(false);

    //}

           });      

       } catch (error) {

           Xrm.Navigation.openAlertDialog("Ok", error.message);

       }

    };

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi, All,

    Above code will work if using xmlhttprequest sync way then its working fine in advanced ribbon.

    Is there any other way avilable to work in asyn way becz advanced find page is web client & here resolve promise is not going to work.

    Thanks,

    Jharana

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 33 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans