Check if AssignedTo has role

heathers_
Kilo Sage

On a field condition, if the assigned_to does not have a specific role, we'd like to clear the assigned_to value. If the assigned_to does have the role, we don't want to clear the value. This is being done through a scoped app. I've taken some suggestions from a few other posts and so far unable to achieve the goal. Currently, the value is clearing regardless of role.

 

Script Include

 

HrRoleUtils = Class.create();
HrRoleUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    hasRole: function(user, role) {
        if (!user) user = this.getParameter('sysparm_user_sys_id');
        if (!role) role = this.getParameter('sysparm_role');
        if (user && role)
            return gs.getUser().getID(user).hasRole(role);
    },
    type: 'HrRoleUtils'

 

 

Client Script

 

if (newValue == 'Yes') {

           var ga = new GlideAjax('sn_hr_core.HrRoleUtils');
        ga.addParam('sysparm_name', 'hasRole');
        ga.addParam('sysparm_user_sys_id', g_form.getValue('assigned_to'));
        ga.addParam('sysparm_role', 'sn_hr_core.u_hr_confidential');


        ga.getXML(function(resp) {
            var response = JSON.parse(resp.responseXML.documentElement.getAttribute('answer'));
            if (response) {
                g_form.clearValue('assigned_to');

            }
        });

 

1 ACCEPTED SOLUTION

 

var HrRoleUtils = Class.create();
HrRoleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRole: function(userSysId, role) {
var userGr = new GlideRecord('sys_user');
if (userSysId && role) {
if (userGr.get(userSysId)) {
return gs.hasRole(role, userGr);
}
}
return false;
},

type: 'HrRoleUtils'
});

 

if (newValue == 'Yes') {
var assignedTo = g_form.getValue('assigned_to');
if (assignedTo) {
var ga = new GlideAjax('sn_hr_core.HrRoleUtils');
ga.addParam('sysparm_name', 'hasRole');
ga.addParam('sysparm_role', 'sn_hr_core.u_hr_confidential');
ga.addParam('sysparm_user_sys_id', assignedTo);

ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer === 'true') {
// User has the specified role, do nothing
} else {
// User doesn't have the role, clear the assigned_to value
g_form.clearValue('assigned_to');
}
});
}
}

View solution in original post

11 REPLIES 11

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @heathers_ , 

 

Try this :

 

Script include : 

var userId = this.getParameter('user_id');
      var user = new GlideRecord('sys_user');
      if (user.get(userId)) {
         if (user.hasRole('sn_hr_core.u_hr_confidential')) {
            return 'has_role';
         } else {
            return 'no_role';
         }
      }

 

Client scritp :

 var ga = new GlideAjax('CheckHRRole');
   ga.addParam('sysparm_name', 'checkUserRole');
   ga.addParam('user_id', g_form.getValue('assigned_to'));
   ga.getXMLAnswer(function(response) {
      if (response == 'no_role') {
         g_form.clearValue('assigned_to');
      }

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Unfortunately this did not work.

Amit Pandey
Kilo Sage

Hi @heathers_ 

 

Can you try following-

Script include-

var HrRoleUtils = Class.create();
HrRoleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    hasRole: function(user, role) {
        var userObj = gs.getUser();
        if (user && role) {
            return userObj.hasRole(role);
        }
        return false;
    },

    type: 'HrRoleUtils'
});

 

Client Script-

 

if (newValue == 'Yes') {
    var ga = new GlideAjax('sn_hr_core.HrRoleUtils');
    ga.addParam('sysparm_name', 'hasRole');
    ga.addParam('sysparm_role', 'sn_hr_core.u_hr_confidential');

    // Using current user's sys_id
    ga.addParam('sysparm_user_sys_id', gs.getUserID());

    ga.getXML(function(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        if (answer === 'true') {
            // User has the specified role, do nothing
        } else {
            // User doesn't have the role, clear the assigned_to value
            g_form.clearValue('assigned_to');
        }
    });
}

Please mark my answer helpful and correct.

 

Regards,

Amit

I want to get the role of the assigned_to, not the logged in user. How should I adjust?

 

var HrRoleUtils = Class.create();
HrRoleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
hasRole: function(userSysId, role) {
var userGr = new GlideRecord('sys_user');
if (userSysId && role) {
if (userGr.get(userSysId)) {
return gs.hasRole(role, userGr);
}
}
return false;
},

type: 'HrRoleUtils'
});

 

if (newValue == 'Yes') {
var assignedTo = g_form.getValue('assigned_to');
if (assignedTo) {
var ga = new GlideAjax('sn_hr_core.HrRoleUtils');
ga.addParam('sysparm_name', 'hasRole');
ga.addParam('sysparm_role', 'sn_hr_core.u_hr_confidential');
ga.addParam('sysparm_user_sys_id', assignedTo);

ga.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
if (answer === 'true') {
// User has the specified role, do nothing
} else {
// User doesn't have the role, clear the assigned_to value
g_form.clearValue('assigned_to');
}
});
}
}