relationship between caller field and role

srinadh123v
Tera Contributor

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

6 REPLIES 6

Tarun Rekandar
Giga Guru

Hi @srinadh123v,

 

For your requirement we need to use Client script, Reference qualifier, Script Include.

  1. Client script: Runs in the browser when the user changes the Role. It clears the Caller field.
  2. Reference qualifier: Runs when the user opens the Caller dropdown. It decides which users to show.
  3. Script Include: Finds the users who have the selected role. The reference qualifier asks it for the list.

Step by Step:

  1. The user picks a Role.
  2. The client script clears the old Caller. This is needed because the old user may not have the new role.
  3. The user clicks the Caller reference field.
  4. The reference qualifier asks the Script Include: “Which users have this role?”
  5. The Script Include looks in the database and returns the list of users.
  6. 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

Aditya_hublikar
Giga Sage

Hello @srinadh123v ,

 

Let me clarify the use case: there are two fields, Role and Caller. When a role is selected, the Caller field should only display users who possess that selected role, correct?
If yes, you will first need to change the Role field's reference table to sys_user_role and ensure the Caller field references the sys_user table.
After that, configure the Caller field by setting the Reference qual (Reference Qualifier Type) to Advanced, and paste the following code into the reference qualifier field:
 
 
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(',');
})();

 

Aditya_hublikar_0-1793538426975.png

 

 

If this helps you then mark it as helpful and accept as solution .

Regards,

Aditya