The Zurich release has arrived! Interested in new features and functionalities? Click here for more

call script include from ui script

Rosy14
Tera Guru

Hi I am getting null. plz help.

script include:

client callable: yes

var CheckUserRole = Class.create();
CheckUserRole.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    u_hasRole: function() {
   var role = this.getParameter("sysparm_role");
   return gs.hasRole(role);

   },
    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();
         alert(ga.getAnswer());
     }
   
})();
2 REPLIES 2

Kumar Premankit
Tera Expert

Inspite of this client script why dont you use "g_user.hasRoleExactly('<roleName>');"

 

Please read the following for detail insight on g_user. 

https://developer.servicenow.com/dev.do#!/learn/courses/utah/app_store_learnv2_scripting_utah_script... 

avieet
Tera Contributor

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.