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

I made that change and in log or console I see the correct CIs logged but in the field itself I see all CIs that are ipc true

 

I suspect it is this    return ciList.join(','); // Return comma-separated sys_ids of the servers  and g_form.setValue('ipc_server', sysIds[0]); // Set the first sys_id as the value

 

so I changed the SI to return 'sys_idIN' +ciList;   but to no avail I still see far to many CIs 
Any suggestion I should return and set it on the variable to only show the valid CIs and also I don't want it to set the field at all so not sure how to stop it set the variable, at least it sets it to a valid CI

Inactive the Catalog Client Script, and any others you might have running onLoad or onChange to take that out of the equation, then the list of records should be the same as the logged ciList.  If you don't need to do anything else, leave it inactivated.  If you still want it to clear any value onLoad if no records are returned, but that's all, then just comment out/remove the setValue line. 

Hi Brad,

 

I tried the above but didn't work either.

 

In my PDI I created a small cat item I am on Xanadu

I logs the correct user sys id, comp sys id and logs the 4 servers the user should see but in the variable all server are listed. I do not understand this at all anymore. The ref variable for ipc_server I tried simple and advanced with and without ref qualifier does this matter?

 

I have done quite a few of these AJAX calls and they always return the expected values so I am baffled and cannot figure out how to solve this.

 

SI

function onLoad() {
var reqUserSysId = g_form.getValue('req_for'); // Get the user from the req_for variable

console.log('req_for value: ' + reqUserSysId); // Log the value of req_for

if (reqUserSysId) {
var ga = new GlideAjax('Icptest'); // Call the Script Include 'Icptest'
ga.addParam('sysparm_name', 'getIPCServersForCurrentUser'); // Specify the function to call
ga.addParam('sysparm_req_for', reqUserSysId); // Pass the req_for user sys_id
 
ga.getXMLAnswer(function(response) {
console.log('Response from server: ' + response); // Log the server response
 
if (response && response.trim() !== '') {
console.log('Setting ipc_servers field with: ' + response); // Log the value being set
g_form.setValue('ipc_servers', response); // Set the value directly
} else {
console.log('No valid response received, clearing field'); // Log if no valid response
g_form.clearValue('ipc_servers'); // Clear if no valid response
}
});
} else {
console.log('No req_for value found, clearing ipc_servers field'); // Log if no req_for is found
g_form.clearValue('ipc_servers'); // Clear the field if no req_user is found
}
}

var Icptest = Class.create();
Icptest.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

getIPCServersForCurrentUser: function() {
var req_for = this.getParameter('sysparm_req_for'); // Get the passed req_for user sys_id

if (!req_for) {
gs.warn('No req_for value passed to Script Include.');
return '';
}
gs.info('Starting getIPCServersForCurrentUser for user: ' + req_for); // Log user sys_id
var result = [];
// Get the company of the current user
var userGR = new GlideRecord('sys_user');
if (userGR.get(req_for)) {
var userCompanySysId = userGR.company.sys_id.toString(); // Get user's company sys_id
gs.info('User found: ' + req_for + ', Company: ' + userCompanySysId); // Log user's company

// Query cmdb_ci_server for servers where u_ipc is true and company matches
var serverGR = new GlideRecord('cmdb_ci_server');
serverGR.addQuery('u_ipc', true); // Only IPC servers
serverGR.addQuery('company', userCompanySysId); // Match user's company sys_id
serverGR.query();

// Collect the sys_ids of the matching records
gs.info('Querying cmdb_ci_server for IPC servers in company: ' + userCompanySysId);
while (serverGR.next()) {
gs.info('Found IPC server: ' + serverGR.getValue('sys_id')); // Log each matching sys_id
result.push(serverGR.getValue('sys_id'));
}
} else {
gs.warn('User not found for sys_id: ' + req_for); // Log if the user is not found
}

gs.info('Returning IPC server sys_ids: ' + result.join(',')); // Log final result
return result.join(','); // Return comma-separated sys_ids
},

type: 'Icptest'
});

 

AJAX

function onLoad() {
var reqUserSysId = g_form.getValue('req_for'); // Get the user from the req_for variable

console.log('req_for value: ' + reqUserSysId); // Log the value of req_for

if (reqUserSysId) {
var ga = new GlideAjax('Icptest'); // Call the Script Include 'Icptest'
ga.addParam('sysparm_name', 'getIPCServersForCurrentUser'); // Specify the function to call
ga.addParam('sysparm_req_for', reqUserSysId); // Pass the req_for user sys_id
 
ga.getXMLAnswer(function(response) {
console.log('Response from server: ' + response); // Log the server response
 
if (response && response.trim() !== '') {
console.log('Setting ipc_servers field with: ' + response); // Log the value being set
g_form.setValue('ipc_servers', response); // Set the value directly
} else {
console.log('No valid response received, clearing field'); // Log if no valid response
g_form.clearValue('ipc_servers'); // Clear if no valid response
}
});
} else {
console.log('No req_for value found, clearing ipc_servers field'); // Log if no req_for is found
g_form.clearValue('ipc_servers'); // Clear the field if no req_user is found
}
}

I'm not understanding why you are doing a CCS/AJAX.  THE way to control the records that are displayed in a reference variable on a Catalog Item / RITM / Catalog Task form, is to use a reference qualifier.  In your case you need to use an advanced one that calls a Script Include.  Start with only that to get the correct records in the list before trying to do anything with a catalog client script or ajax call.  Use the advanced reference qualifier and script include version with req_for passed in from the reference qualifier that I proposed.

I am sorry to be so troublesome but I tried it with ref qual as below

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

 

and AJAX call as you suggested but either way I see all servers so not sure how to solve this now. so that it will only show the servers ipc true for the requesters company.