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

  • erhan.keskin Profile Picture
    erhan.keskin 2,253 on at
    RE: JS: Reference Error

    Hi,

    It is missing a ")" in this line;

    if (UserHasRole("System Administrator") {

    should be like this;

    if (UserHasRole("System Administrator")) {

    Regards,

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: JS: Reference Error

    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.

  • necsa Profile Picture
    necsa 3,455 on at
    RE: JS: Reference Error

    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();
    }

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans