On a ribbon button I have the following JS to detect the role of the user. I have been reading that Xrm.Page.Context is deprecated, however if I try updating the script below to use Global Context, it always returns undefined and doesn't work. This is my present code that works (please suggest how to update):
function getrolesarray(formcontextfromribbon) { var Globalcontext = Xrm.Page.context; var strFirstThree = Globalcontext.getVersion().substring(0, 3); var sameunit = true; var noclosed_status = true; var selview = 0; var roleid = Globalcontext.getUserRoles(); var RoleName_concat = ""; var user_sysadmin_yesno = false; var name; for (var i = 0; i < roleid.length; i ) { var roleID = roleid[i]; var RoleName = getRoleName_XMLHttp(roleID,strFirstThree); RoleName = RoleName.toUpperCase() if ((RoleName == 'SYSTEM ADMINISTRATOR')) { user_sysadmin_yesno = true; break; } } return user_sysadmin_yesno; } function getRoleName_XMLHttp(roleID,strFirstThree) { var roleName = null; var param = roleID.toString().replace("{", "").replace("}", ""); var ajaxurl = Globalcontext.getClientUrl() "/api/data/v" strFirstThree "/roles(" param ")?$select=_businessunitid_value,name,roleid"; var req = new XMLHttpRequest(); req.open("GET", ajaxurl, false); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.setRequestHeader("Prefer", "odata.include-annotations=\"*\""); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); var _businessunitid_value = result["_businessunitid_value"]; var _businessunitid_value_formatted = result["_businessunitid_value@OData.Community.Display.V1.FormattedValue"]; var _businessunitid_value_lookuplogicalname = result["_businessunitid_value@Microsoft.Dynamics.CRM.lookuplogicalname"]; var name = result["name"]; var roleid = result["roleid"]; roleName = result["name"]; } else { Xrm.Utility.alertDialog(this.statusText); } } }; req.send(); return roleName; }
var Globalcontext = Xrm.Utility.getGlobalContext();