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
Yes, this is a dependent-reference case. A static reference qualifier on the caller field will not do it on its own, because the caller is on sys_user and the role assignment lives in the many-to-many table sys_user_has_role. Filter the caller lookup after the role is chosen, rather than trying to hard-code the qualifier.
General pattern:
1. Keep u_role as a reference to sys_user_role, so its value on the form is the role sys_id.
2. Add an onChange Client Script on u_role. When the role changes, clear the caller field, then call a client-callable Script Include with GlideAjax.
3. In the Script Include, query sys_user_has_role where role is the selected role, collect the user sys_ids, and return them as a comma-separated list.
4. Back in the Client Script, set the caller reference qualifier to sys_idIN followed by that list. The lookup then shows only users who have that role.
Client Script, onChange on u_role (adjust the caller field name if yours is not caller_id):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || !newValue) {
g_form.clearValue('caller_id');
g_form.setReferenceQualifier('caller_id', '');
return;
}
g_form.clearValue('caller_id');
var ga = new GlideAjax('RoleUserLookup');
ga.addParam('sysparm_name', 'getUsersForRole');
ga.addParam('sysparm_role_id', newValue);
ga.getXMLAnswer(function(answer) {
var qualifier = answer ? 'sys_idIN' + answer : 'sys_idISEMPTY';
g_form.setReferenceQualifier('caller_id', qualifier);
});
}
Script Include, client-callable, named RoleUserLookup (add the application scope prefix to the GlideAjax name if this is in a scoped application):
var RoleUserLookup = Class.create();
RoleUserLookup.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUsersForRole: function() {
var roleId = this.getParameter('sysparm_role_id');
var userIds = [];
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('role', roleId);
gr.query();
while (gr.next()) {
userIds.push(gr.getValue('user'));
}
return userIds.join(',');
},
type: 'RoleUserLookup'
});
Two things to check before using it: if u_role stores a role name rather than a sys_id, resolve it through sys_user_role first, or change u_role to a sys_user_role reference. And always clear the caller when the role changes, otherwise a previously selected caller can stay on the form even though it no longer matches the role. Test this in a sub-production instance first.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @srinadh123v
Can you try to give some context into the user case? What is the business requirement that you're trying to fulfill?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @srinadh123v
I tried this on my PDI and it works with a dynamic reference qualifier on the caller field, so you dont need a client script for the filtering.
1. Create a script include (no need for client callable):
var RoleUserLookup = Class.create();
RoleUserLookup.prototype = {
initialize: function() {},
getUsersByRole: function(roleId) {
var ids = [];
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('role', roleId);
gr.query();
while (gr.next()) {
ids.push(gr.getValue('user'));
}
return ids.join(',');
},
type: 'RoleUserLookup'
};2. On the caller fields dictionary entry, set the reference qualifier to Advanced:
javascript:'sys_idIN' + new RoleUserLookup().getUsersByRole(current.u_role)3. Set the dependent field on the caller to 'u_role', so the lookup refreshes when the role changes.
4. Add an onChange client script on "u_role" that just does g_form.clearValue('<your caller field>'), so an old caller doesnt stay after the role changes.
After this, the lookup lists only users who have the selected role. Let me know which table you are using if it does not work.
** If this helps, please mark it "helpful" 😊 and accept it as the solution if it resolves your issue. **
Regards
Dimple Shaik.
- 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