advanced referene qualifier

Eli7
Tera Expert

Hi All,

 

I have an advanced ref qualifier and I am calling a SI 

Javascript:new NewIPC().getIPCServersForCurrentUser(current.variables.req_for);

 

In my SI I am not sure how to receive or map the var req_for. I am only trying this because in my AJAX call it return the expected values but also other record form the CMDB_CI and I am not quite sure why.

 

Any input appreciated.

1 ACCEPTED SOLUTION

I missed earlier that it looks like you are capitalizing the J.  It's hard to tell with this editor substitutions, but it should look like this:

BradBowman_0-1728479573655.png

Confirm the reference table for this variable is cmdb_ci_server, and that have a variable named req_for that is a reference to sys_user, then you should see the selected user sys_id logged in the Script Include.  If the logic in the SI is correct, you should see the correct records logged before the return, then the same should show in the list on the reference variable.  None of this is using or requires AJAX or a Catalog Client Script.

View solution in original post

14 REPLIES 14

Brad Bowman
Kilo Patron
Kilo Patron

Just use a name in the function declaration as an argument, then that name will be available as a script variable

getIPCServersForCurrentUser: function(req_for) {
    gs.info('Current user = ' + req_for);
}

Post your SI and more about why you were trying an AJAX call if you're still stuck.

The requirement is to return server that are ipc true and are in the same company the requester is in and that the requester is in the ipcadmin group in my console.log I see the 3 servers that are available for this company but in the variable it self it shows all servers in cmdb_ci_server 

This was with Ajax call and here is the code

 

var NewIPC = Class.create();
NewIPC.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
    // Function to fetch IPC servers for the specified user
    getIPCServersForCurrentUser: function() {
     var userSysId = this.getParameter('sysparm_userSysId'); // Get the passed user sys_id
 
        var ipcAdminGroupSysId = gs.getProperty('ipcAdminGroup'); // Get the IPC Admin group sys_id
        var userRecord = new GlideRecord('sys_user');
        if (!userRecord.get(userSysId)) {
            gs.info('User not found: ' + userSysId);
            return ''; // Return empty if user is not found
        }
        var userCompany = userRecord.company.sys_id; // Get the company of the user
        // Check if the user is in the IPC Admin group
        if (!this.isUserInGroup(userSysId, ipcAdminGroupSysId)) {
            gs.info('User is not in the IPC Admin group');
            return ''; // Return empty if user is not in the IPC Admin group
        }
        gs.info('User is in the IPC Admin group. Fetching IPC servers...');
 
        // Fetch only IPC servers (u_ipc=true) for the specified user's company
        var ciList = [];
        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('u_ipc', true); // Ensure we are only fetching IPC servers
        gr.addQuery('company', userCompany); // Ensure servers belong to the same company as the user
        gr.query();
 
        while (gr.next()) {
            ciList.push(gr.sys_id.toString()); // Collect sys_id
    gs.info('User is in the IPC Admin group. Fetching IPC ciList...' +ciList);
        }
        return ciList.join(','); // Return comma-separated sys_ids of the servers
    },
 
    // Helper function to check if the user is in the IPC Admin group
    isUserInGroup: function(userSysId, groupSysId) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userSysId);
        gr.addQuery('group', groupSysId);
        gr.query();
 
        return gr.hasNext(); // Return true if user is in the group
    },
 
    type: 'NewIPC'
});
 
 
function onLoad() {
    var reqUserSysId = g_form.getValue('hidden_req_for'); // Get the user from the hidden_req_for variable
 
    if (reqUserSysId) {
        // GlideAjax call to get IPC servers for the specified user
        var ga = new GlideAjax('NewIPC'); // Call the Script Include 'NewIPC'
        ga.addParam('sysparm_name', 'getIPCServersForCurrentUser'); // Specify the function to call
        ga.addParam('sysparm_userSysId', reqUserSysId); // Pass the hidden_req_for user sys_id
        ga.getXMLAnswer(function(response) {
            if (response && response.trim() !== '') {
                var sysIds = response.split(','); // Split the comma-separated sys_ids into an array
 
                // Set the first IPC server as the value for the target_device field
                if (sysIds.length > 0) {
console.log('newEllls ' + sysIds);
                    g_form.setValue('ipc_server', sysIds[0]); // Set the first sys_id as the value
                }
            } else {
                // If no servers are returned, clear the field
                g_form.clearValue('ipc_server');
            }
        });
    } else {
        g_form.clearValue('ipc_server'); // Clear the field if no req_user is found 
    }
}
 
I disabled the CCS and tried with below advanced ref qualifier but didn't get it work how I need it to.
 
Javascript:new  NewIPC().getIPCServersForCurrentUser(current.variables.hidden_req_for);
 
and now I have lost the plot so thank you for your help!

Hi Brad, how exactly would I use this 

 

getIPCServersForCurrentUser: function(req_for) {
    gs.info('Current user = ' + req_for);
}

 

 

   getIPCServersForCurrentUser: function(req_for) {
     var userSysId = how do I assign the req_for  to the var?  I tried a few meaningless things but nothing worked

You can use the same function for client or server callable like this:

 

getIPCServersForCurrentUser: function(req_for) {
     var userSysId = this.getParameter('sysparm_userSysId') || req_for; // Get the passed user sys_id from CCS or reference qualifier

 

So userSysId will be the value of the parameter from the CCS if the script was called with that, or the value passed in from the reference qualifier.  When reference qualifiers resolve, the format required is 'sys_idIN1....,2...,3....' with the numbers being a list of sys_ids on the referenced table.  Since your function is only returning a list of sys_ids to work with the GA call, your reference qualifier should be:

 

javascript: 'sys_idIN' + new NewIPC().getIPCServersForCurrentUser(current.variables.req_for);