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

JS: Reference Error

(0) ShareShare
ReportReport
Posted on by 17

Hello Everyone,

 I wrote a js to hide the lookup field on the form if the user is not System Admin . After saving it is throwing the reference error 

Error:

ReferenceError: 'OnLoad' is undefined
at eval code (eval code:1:1)
at RunHandlerInternal (xxxxxx.dynamics.com/.../ClientApiWrapper.aspx
at RunHandlers (xxxxxxx.dynamics.com/.../ClientApiWrapper.aspx
at OnScriptTagLoaded (xxxxxxxx.dynamics.com/.../ClientApiWrapper.aspx
at Anonymous function (https://xxxxxxxxx.dynamics.com/form/ClientApiWrapper.aspx?ver=-202171222:244:1)

JS script:

function OnLoad() {
if (UserHasRole("System Administrator") {
Xrm.Page.ui.controls.get("field_name").setVisible(true);

} else {
Xrm.Page.ui.controls.get("field_name").setVisible(false);
}
}

function UserHasRole(RoleName) {
var currentUserRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < currentUserRoles.length; i++) {
var userRoleId = currentUserRoles[i];
var userRoleName = GetRoleName(userRoleId);
if (userRoleName == RoleName) {
return true;
}
}
return false;
}

function GetRoleName(roleId) {
var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
var roleName = null;
$.ajax(
{
type: "GET",
async: false,
contentType: "application/json; charset=utf-8",
datatype: "json",
url: odataSelect,
beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
success: function (data, textStatus, XmlHttpRequest) {
roleName = data.d.results[0].Name;
},
error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
}
);
return roleName;
}

Please help me to resolve the error 

Thanks In Advance

I have the same question (0)
  • necsa Profile Picture
    3,455 on at

    Hi Bhavya,

    I don't know which version CRM do you have but please try following code Onload event.

    function CheckUserRole() {
    var _roleQuery = '';
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {

    if (i == 0)
    _roleQuery += 'roleid eq ' + currentUserRoles[i];
    else
    _roleQuery += ' or roleid eq ' + currentUserRoles[i];

    }

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/roles?$select=name" + '&$filter=' + _roleQuery, true);
    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 results = JSON.parse(this.response);
    for (var i = 0; i < results.value.length; i++) {
    var roleName = results.value[i]["name"];
    if (roleName == "System Administrator") {

    ///perform a task in your case

    if (UserHasRole("System Administrator") {
    Xrm.Page.ui.controls.get("field_name").setVisible(true);

    } else {
    Xrm.Page.ui.controls.get("field_name").setVisible(false);
    }

    }
    }
    } else {
    alert(this.statusText);

    }
    }
    };
    req.send();
    }

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Most of the time, "ReferenceError: '<MethodName>' is undefined" error message occurs due to the syntax error in the javascript liabrary. I checked the script you have shared with the online JS syntax validator and it does have syntax error i.e. you are missing ")" in the if statement

    https://esprima.org/demo/validate.html

    Replace below

    if (UserHasRole("System Administrator") {

    with

    if (UserHasRole("System Administrator")){

    Hope this helps.

  • erhan.keskin Profile Picture
    2,253 on at

    Hi,

    It is missing a ")" in this line;

    if (UserHasRole("System Administrator") {

    should be like this;

    if (UserHasRole("System Administrator")) {

    Regards,

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 73 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 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