Return sys id from glide ajax and populate in list collector.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 03:08 AM
Hello Experts,
We have stored some sys IDs in system properties separated by ",".
Do we need to use glide ajax then? Cause we want to return sys ids which are stored in system property such that we can use them to populate in List collector.
Kindly help us with the following code:
Script include
initialize: function() {
var a = [];
var services = gs.getProperty("test")
var recGrp = new GlideRecord('pc_software_cat_item');
recGrp.addQuery('sys_id', 'IN', services); //sysid is one of property - filter
recGrp.query();
while (recGrp.next()) {
a.push(recGrp.toString());
}
gs.info("test" + a);
return 'sys_idIN' + a;
},
###
client script
function ajaxResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('required_software', answer);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 03:13 AM
Hi @tanmay8 ,
Hope you are doing good.
Could you please let us know the use case. I mean the population needs to happen on load or on change and also are we doing if for a catalog item or for a table.
Regards,
Harshal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 03:18 AM
Hello Harshal,
Thanks for the quick response.
Its catalog item and for Onchange client script.
Regards,
Tanmay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 03:51 AM
Hi @tanmay8 ,
Yes we would need to use glide ajax for the same please refer to the below code-
I have taken the example of sys_user table, you might need to adjust the code
Script Include
var populateList = Class.create();
populateList.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getList: function() {
var ids = "";
var getUser = new GlideRecord("sys_user");
getUser.addQuery("sys_id","IN",gs.getProperty("test_populate"));
getUser.query();
while(getUser.next()){
ids+=getUser.manager+",";
}
return ids;
}
});
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue) // Please update the condition as per your requirement
{
var populate = new GlideAjax("populateList"); // Name of the script include
populate.addParam("sysparm_name", "getList"); // Name of the function
populate.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue("test_list",answer);
});
}
}
Please Mark My Response as Correct/Helpful based on Impact
Regards,
Harshal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-22-2023 04:06 AM
Hi
Yes , we need to use glide ajax call to get the sys_ids from system property.
I have not gone through your code, I am just pasting the code. Hope that helps.
Script include method -
getOpportunities: function() {
var opportunities = [];
var opportunityGroup = this.getParameter('sysparm_opportunityGroup');
var gr = new GlideRecord('table_name');
gr.addEncodedQuery('field_name='+opportunityGroup.toString());
gr.query();
while(gr.next()){
opportunities.push(gr.field_name.toString());
}
return opportunities.toString();
}
Client Script - Focus on glide Ajax call
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
removeValues();
} else {
removeValues();
var left, right, l_o;
//Glide Ajax Call
var ga = new GlideAjax('Script_include_name');
ga.addParam('sysparm_name', 'getOpportunities');
ga.addParam('sysparm_opportunityGroup', g_form.getValue('field_name'));
ga.getXML(getOpportunity);
function getOpportunity(response) {
var data = response.responseXML.documentElement.getAttribute("answer");
var ids = data.split(',');
setSlushBucket('u_opportunity', 'sys_idIN' + ids.toString());
g_form.setDisplay('u_opportunity', false);
setCollectorFilter();
function setCollectorFilter() {
//get the current showing list from 'available' and 'selected' slush bucket
left = gel('slush_bucket_name' + '_select_0');
right = gel('slush_bucket_name' + '_select_1');
l_o = left.options;
//condition to check - available slush bucket should have the records according to new filter set.
if (l_o.length != ids.length) {
setTimeout(setCollectorFilter, 100); // setting timeout untill the available slush bucket get refreshed.
return;
} else { // when 'available' slush bucket is refreshed - control will come to else block where we are setting the necessary records in 'selected' slush bucket
g_form.setValue('field_name', ids.toString());
setSlushBucket('u_opportunity', 'field_name=10f2246fdb11909064cb5eea4b96194b'); //reset the fiter to reset the available slush bucket
g_form.setDisplay('u_opportunity', true);
}
}
}
}
}
function removeValues() {
g_form.setValue('u_opportunity', '');
}
//Below function will set the filter of the list collectors account to the query passed in the param
function setSlushBucket(collectorName, sqlQuery) {
window[collectorName + 'g_filter'].reset();
window[collectorName + 'g_filter'].setQuery(sqlQuery);
window[collectorName + 'acRequest'](null);
}