- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-02-2024 11:31 PM - edited 04-02-2024 11:31 PM
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');
}
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:45 AM
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');
}
});
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 12:35 AM
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....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:23 AM
Unfortunately this did not work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 12:35 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:24 AM
I want to get the role of the assigned_to, not the logged in user. How should I adjust?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-03-2024 01:45 AM
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');
}
});
}
}