locked
Intermittant Javascript Error RRS feed

  • Question

  • See below code block, I get an intermittant error on the below code on all forms.  F12 in IE indicates that the problem lies at the line in bold and italics.  It sometimes works fine, sometimes throws up a syntax error.  Any ideas?

    function HideSection() {
        if(UserHasRole("Operator") == true) {
                    Xrm.Page.ui.tabs.get("Operation").sections.get("Section1").setVisible(true);
    }
    
    function UserHasRole(roleName) {
        var serverUrl = Xrm.Page.context.getServerUrl();
        var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
        var org = Xrm.Page.context.getOrgUniqueName()
        var oDataEndpointUrl = "/" + org + "/XRMServices/2011/OrganizationData.svc/";
        oDataEndpointUrl += "RoleSet?$top=1&$filter=Name eq '" + roleName + "'";
        var service = GetRequestObject();
        if (service != null) {
            service.open("GET", oDataEndpointUrl, false);
            service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
            service.setRequestHeader("Accept", "application/json, text/javascript, */*");
            service.send(null);
            var requestResults = eval('(' + service.responseText + ')').d;
            if (requestResults != null && requestResults.results.length == 1) {
                var role = requestResults.results[0];
                var id = role.RoleId;
                var currentUserRoles = Xrm.Page.context.getUserRoles();
                for (var i = 0; i < currentUserRoles.length; i++) {
                    var userRole = currentUserRoles[i];
                    if (GuidsAreEqual(userRole, id)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    function GetRequestObject() {
        if (window.XMLHttpRequest) {
            return new window.XMLHttpRequest;
        } else {
            try {
                return new ActiveXObject("MSXML2.XMLHTTP.3.0");
            } catch (ex) {
                return null;
            }
        }
    }
    
    function GuidsAreEqual(guid1, guid2) {
        var isEqual = false;
        if (guid1 == null || guid2 == null) {
            isEqual = false;
        } else {
            isEqual = guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase();
        }
        return isEqual;
    }


    Tuesday, September 17, 2013 1:01 PM

Answers

  • What is the error? 

    You could also try this version:

    function IsUserInRole(roleName) {
        var currentUserRoles = Xrm.Page.context.getUserRoles();
        var serverUrl = Xrm.Page.context.getClientUrl();
        var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=RoleId&$filter=Name eq '" + roleName + "'&$top=1";
        var result = false;
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        retrieveReq.onreadystatechange = function () {
            if (retrieveReq.readyState === 4) {
                if (retrieveReq.status === 200) {
                    var retrieved = JSON.parse(retrieveReq.responseText).d;
                    if (retrieved.results.length > 0) {
                        for (var i = 0; i < currentUserRoles.length; i++) {
                            if (GuidsAreEqual(currentUserRoles[i], retrieved.results[0].RoleId)) {
                                result = true;
                            }
                        }
                    }
                }
            }
        };
    
        retrieveReq.send();
        return result;
    };
    
    function GuidsAreEqual(guid1, guid2) {
        var isEqual;
        isEqual = false;
        if (guid1 == null || guid2 == null) {
            isEqual = false;
        } else {
            isEqual = (guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase());
        }
        return isEqual;
    };


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    • Marked as answer by RoarCRM Monday, December 2, 2013 3:53 PM
    Tuesday, September 17, 2013 1:10 PM
    Moderator