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

The field keeps clearing for users with the role. It doesn't seem to be evaluating properly.

heathers_
Kilo Sage

Moving my script to global instead of scoped resolved the issue. Thank you all for your help.