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