relationship between caller field and role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
how to make custom form, i mean first select u_role(reference qualifier has = 'sys-user-has-role'), second caller field click on lookup then show users list based on u_role field with coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @srinadh123v,
For your requirement we need to use Client script, Reference qualifier, Script Include.
- Client script: Runs in the browser when the user changes the Role. It clears the Caller field.
- Reference qualifier: Runs when the user opens the Caller dropdown. It decides which users to show.
- Script Include: Finds the users who have the selected role. The reference qualifier asks it for the list.
Step by Step:
- The user picks a Role.
- The client script clears the old Caller. This is needed because the old user may not have the new role.
- The user clicks the Caller reference field.
- The reference qualifier asks the Script Include: “Which users have this role?”
- The Script Include looks in the database and returns the list of users.
- The Caller reference field shows only those users.
onChange Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
g_form.clearValue('u_caller');
if (newValue === '') {
return;
}
var ga = new GlideAjax('CallerRoleUtil');
ga.addParam('sysparm_name', 'countUsersByRole');
ga.addParam('sysparm_role', newValue);
ga.getXMLAnswer(function(answer) {
if (parseInt(answer, 10) === 0) {
g_form.addInfoMessage('No active users have this role.');
}
});
}
Advanced Reference qualifier:
javascript:new CallerRoleUtil().getUsersByRole(current.u_role)
Client-Callable Script Include:
var CallerRoleUtil = Class.create();
CallerRoleUtil.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
// Called from the reference qualifier (server side)
getUsersByRole: function(roleValue) {
var users = this._getUserIds(roleValue);
return users.length ? 'sys_idIN' + users.join(',') : 'sys_id=-1';
},
// Called from the client via GlideAjax
countUsersByRole: function() {
var role = this.getParameter('sysparm_role');
return this._getUserIds(role).length;
},
_getUserIds: function(roleValue) {
var users = [];
if (!roleValue){
return users;
}
var ga = new GlideAggregate('sys_user_has_role');
ga.addQuery('state', 'active');
ga.addQuery('user.active', true);
if (/^[0-9a-f]{32}$/.test(roleValue)){
ga.addQuery('role', roleValue); // sys_id
}else{
ga.addQuery('role.name', roleValue); // role name
}
ga.groupBy('user');
ga.query();
while (ga.next()){
users.push(ga.getValue('user'));
}
return users;
},
type: 'CallerRoleUtil'
});
Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards,
Tarun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hello @srinadh123v ,
javascript:( function() {
var userRole = current.getValue('u_role');
var userList = [];
var users = new GlideRecord('sys_user_has_role');
users.addQuery('role', userRole);
users.query();
while (users.next()) {
userList.push(users.getValue('user'));
}
if (userList.length === 0) {
return 'sys_idIN';
}
return 'sys_idIN' + userList.join(',');
})();
If this helps you then mark it as helpful and accept as solution .
Regards,
Aditya