locked
JScript to adjust form by User Security Role RRS feed

  • Question

  • Hopefully someone can help.

    I am trying to write some JScript to update a field on a form based on if the user has the relevant security role.
    I have done some searches online but I just cant get the code to work. As I am fairly new to Java can someone please help?
    Currently on CRM 2015 Online.

    Error Message:

    One of the scripts for this record has caused an error. For more details, download the log file.
    ReferenceError: ShowHideDispatchSection is not defined at eval (eval at RunHandlerInternal (https://lvlissdev.crm4.dynamics.com/form/ClientApiWrapper.aspx?ver=941619645:150:32), <anonymous>:1:1)

    Below is my code:

    function ShowHideDispatchSection() {
        if (UserHasRole("System Administrator")) {
    Xrm.Page.getAttribute("liss_lifearea").setValue(131090003);
    }
        } else {
    Xrm.Page.getAttribute("liss_lifearea").setValue(131090000);

        }
    }

    function UserHasRole(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 + "'";
        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++) {
                            for (var j = 0; j < retrieved.results.length; j++) {
                                if (GuidsAreEqual(currentUserRoles[i], retrieved.results[j].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;
    };

    Thursday, March 17, 2016 12:27 PM

Answers

  • Hello,

    Looks like you have 1 extra bracket in function ShowHideDispatchSection. Try to use following code instead:

    function ShowHideDispatchSection() {
        if (UserHasRole("System Administrator")) {
            Xrm.Page.getAttribute("liss_lifearea").setValue(131090003);
    
        } else {
            Xrm.Page.getAttribute("liss_lifearea").setValue(131090000);
    
        }
    }


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Friday, March 18, 2016 12:24 PM
    Moderator

All replies

  • Hi,

    How many js libraries are loaded when you open the form? The error message says that the function doesn't exist yet so the js library doesn't seem to be loaded.

    Regards


    Rickard Norström Developer CRM-Konsulterna
    http://www.crmkonsulterna.se
    Swedish Dynamics CRM Forum: http://www.crmforum.se
    My Blog: http://rickardnorstrom.blogspot.se

    Thursday, March 17, 2016 3:24 PM
  • Hello,

    Looks like you have 1 extra bracket in function ShowHideDispatchSection. Try to use following code instead:

    function ShowHideDispatchSection() {
        if (UserHasRole("System Administrator")) {
            Xrm.Page.getAttribute("liss_lifearea").setValue(131090003);
    
        } else {
            Xrm.Page.getAttribute("liss_lifearea").setValue(131090000);
    
        }
    }


    Dynamics CRM MVP
    Read My blog
    Subscribe for one of my courses

    Friday, March 18, 2016 12:24 PM
    Moderator
  • I have a few running, one that's adjusts the start time and another then makes the window full screen.

    All my code above is one Web Resource as is called once by the initial function ShowHideDispatchSection. Should these be called separately on loading?

    Monday, March 21, 2016 9:03 AM
  • Thank you, this now seems to be working.

    Monday, March 21, 2016 9:08 AM