- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 06:01 AM
Hi All,
I need help with an ajax call parsing values.
I have a script include that works correctly and returns the list of sys ids from the same company as the requester but the CCS I am not able to parse the results. I need to limit the list of available users to be in the same company as the contact person.
Any help greatly appreciated.
CCS
SI
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 09:36 AM
You cannot set a reference qualifier with a Catalog Client Script. Well, there is a way using a Script Include to putClientData in the session, but then the reference qualifier needs changed to get the session data, so as long as you're changing the qualifier...
Assuming the two qualifiers are entirely distinct, since u_collective_identifier wasn't mentioned in your new script, the cleanest way to do this is to change the qualifier in the variable set to:
javascript:new global.getManagerUsers().getUsers(current.variables.cat_item_id, current.variables.ref_contact_person);
So we're passing in another variable value, and removing the criteria for active and collective_identifier which we will incorporate into the Script Include like you did for active on the new script. The idea for the Script Include is that the same function now includes both GlideRecords, but only runs the new one (first) if the ref_contact_person was passed in. If this variable name is not and cannot be made unique to this Catalog Item to distinguish it from the ~87 others then you can also pass in the Catalog Item sys_id using g_form.getUniqueValue(). The Script Include would now look more like this, or whatever makes sense to you knowing what each script was doing:
var getManagerUsers = Class.create();
getManagerUsers.prototype = {
initialize: function() {},
getUsers: function(cat_id, contactPersonSysId, catItemId) {
if (contactPersonSysId) { //or catItemId=..., or may re-use !isValid?...
var userList = [];
// Get the company of the contact person
var userGR = new GlideRecord('sys_user');
if (userGR.get(contactPersonSysId)) {
var contactPersonCompany = userGR.company; // Get the company of the contact person
// Query users in the same company
var gr = new GlideRecord('sys_user');
gr.addQuery('company', contactPersonCompany);
gr.addQuery('active', true); // Filter only active users
gr.query();
// Collect sys_ids of users in the same company
while (gr.next()) {
userList.push(gr.sys_id.toString()); // Add the sys_id of the user to the list
}
}
// Log the response before returning for debugging
gs.info('Returning sys_ids: ' + userList.join(',')); // Logs the array string of sys_ids
// Return the list as a JSON string
return 'sys_idIN' + userList.join(','); // Convert to array string and return it
}
var items = gs.getProperty('hidden_req_for_ref_qual_item_ids').split(','); //Store Item ids to apply ref qual
var isValid = items.indexOf(cat_id.toString()) != -1;
if (isValid) {
var tab = [];
var grUser = new GlideRecord('sys_user');
grUser.addQuery('manager', gs.getUserID());
grUser.addQuery('active', true);
grUser.addQuery('u_collective_identifier', false);
grUser.query();
while (grUser.next()) {
tab.push(grUser.getUniqueValue());
}
return "sys_idIN" + tab;
} else {
return "";
}
},
type: 'getManagerUsers'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 06:14 AM - edited 12-12-2024 06:33 AM
Hi Ellie,
To clarify, if you want to limit the list of available records, you would call your Script Include from a reference qualifier. Your CCS is attempting to set the value of the reference variable to multiple values which only works with List Collector type variables, so first decide which you really want to do.
In either case, in your Script Include, you are pushing the sys_ids to a simple array, which is not a JSON object, and you don't need to worry about duplicates, so use this simplified approach instead:
while (gr.next()) {
userList.push(gr.sys_id.toString());
}
// Log the response before returning for debugging
gs.info('Returning sys_ids: ' + userList.join(',');
// Return the list
return userList.join(','); // Convert array to string and return it
},
type: 'GetUsersFromCompany'
});
Note that if using this with a reference qualifier which only calls the Script Include, you'll want to change the return:
return 'sys_idIN' + userList.join(','); // Convert array to string and return it
If you change the reference variable to a list collector to set the value, just use setValue with the response.
To limit the available records, your reference qualifier would look like this:
If this is a field on a table/form instead of a variable in a Catalog Item, just use current.ref_contact_person.
Your Script Include doesn't need to be Client callable in this case, but it doesn't hurt to leave the extended object in there. Here's what it would look like to work with the user sys_id passed in from the reference qualifier, along with the above changes:
var GetUsersFromCompany = Class.create();
GetUsersFromCompany.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
// Define the getList function to get the list of user sys_ids
getList: function(contactPersonSysId) {
gs.info('Script Include - Contact Person sys_id: ' + contactPersonSysId); // Log the contact person sys_id
var userList = [];
// Get the company of the contact person
var userGR = new GlideRecord('sys_user');
if (userGR.get(contactPersonSysId)) {
var contactPersonCompany = userGR.company; // Get the company of the contact person
// Query users in the same company
var gr = new GlideRecord('sys_user');
gr.addQuery('company', contactPersonCompany);
gr.addQuery('active', true); // Filter only active users
gr.query();
// Collect sys_ids of users in the same company
while (gr.next()) {
userList.push(gr.sys_id.toString()); // Add the sys_id of the user to the list
}
}
}
// Log the response before returning for debugging
gs.info('Returning sys_ids: ' + userList.join(',')); // Logs the array string of sys_ids
// Return the list as a JSON string
return 'sys_idIN' + userList.join(','); // Convert to array string and return it
},
type: 'GetUsersFromCompany'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 07:27 AM
Hi Brad, thanks a ton for your help. My initial idea was to use advanced reference qualifer and your suggestion works great I tried it in my PDI but the catalog item is using a shared variable set and there already is an advanced reference qualifer to filter out what is not an actual user and since its shared with some 87 other catalog items I cannot use a ref qualifier for my use case which is why I though I do it with a catalog client script.
is there a way to push your suggested ref qualifier for a particular catalog item and override the one currently in the field or may add a condition to fire the one for all or the unique one for 1 particular use case?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 08:14 AM
There should be a way to make this work, without adversely affecting the other Catalog Items used by the variable set, but it might depend on what the existing qualifier and Script Include looks like. The fallback workaround is to hide this variable on the one Catalog Item and create a new variable with the different reference qualifier, but that could lead to issues if there's any reporting or workflow dependencies on this variable. Let me know what the variable set qualifier and script look like.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-12-2024 08:33 AM
Below is the current ref qual and SI. It crossed my mind also to possible create a new var for this there is a small flow and updating this would not be an issue but I would need to clear this with the customer first.
Ideally I would like to achieve this with an onLoad CCS so not make too many changes.
this is the reference qualifier: javascript:new global.getManagerUsers().getUsers(current.variables.cat_item_id) + '^active=true' + '^u_collective_identifier=false';
SI