call script include from ui script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 07:05 AM
Hi I am getting null. plz help.
script include:
client callable: yes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 05:31 PM
Inspite of this client script why dont you use "g_user.hasRoleExactly('<roleName>');"
Please read the following for detail insight on g_user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2024 06:07 PM
It appears that your script is returning null because the gs.hasRole() function does not return a value compatible with the expected response from a GlideAjax call. The gs.hasRole() function does not return a string that can be directly read by ga.getAnswer() in your UI script.
Try this modified script:
Script include:
var CheckUserRole = Class.create();
CheckUserRole.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
u_hasRole: function() {
var role = this.getParameter("sysparm_role");
// Check if user has the specified role
return gs.hasRole(role) ? 'true' : 'false';
},
type: 'CheckUserRole'
});
UI Script:
(function() {
userHasRole('admin');
function userHasRole(role) {
var ga = new GlideAjax('CheckUserRole');
ga.addParam('sysparm_name', 'u_hasRole');
ga.addParam('sysparm_role', role);
ga.getXMLWait();
var response = ga.getAnswer();
// Convert the response to boolean
var hasRole = response === 'true';
// Display a message based on whether the user has the role or not
if (hasRole) {
alert("User has the role: " + role);
} else {
alert("User does not have the role: " + role);
}
}
})();
Let me know if that worked for you.