- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 09:50 AM
Hey all I have a good one today, so we created a field on our business service table tgms and BU - CIOs both are list fields
We have a catalog item where both TGM and BU CIO fields are listed as list collector variables that we want to auto populate based on when someone chooses a business service on the form as you can see below:
I have written a script include and catalog script to accomplish this but I am getting no joy and a unhandled glide Ajax error in browser console to boot. Any help here would be appreciated, below are my script includes and client scripts.
Client Script:
/*
Auto-populating TGM users from business Service
*/
function onChange(control, oldValue, newValue, isLoading) {
// give here the list collector variable name
g_form.clearValue('u_tgm_approver');
var parm = '';
if (window === null)
parm = 'portal';
else
parm = 'native';
var ajax = new GlideAjax('ConduentAJAX');
ajax.addParam('sysparm_name', 'getApprovers');
ajax.addParam('sysparm_bs', newValue);
ajax.addParam('sysparm_view', parm);
ajax.getXML(populateValues);
}
function populateValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var arr = answer.split('||');
// give here the list collector variable name
if (window === null) {
g_form.setValue('u_tgm_approver', arr[0]); // Mobile/Portal Compatible
} else {
addItemstoList('u_tgm_approver', answer); // Native Compatible
}
}
function addItemstoList(listCollector, userList) {
var parser = JSON.parse(userList);
var arrSysId = [];
var arrName = [];
for(var i=0;i<parser.length;i++){
arrName.push(parser[i].name.toString());
arrSysId.push(parser[i].sys_id.toString());
}
var varName = listCollector;
var leftBucket = gel(varName + '_select_0');
var rightBucket = gel(varName + '_select_1');
var rightOptions = rightBucket.options;
var rightIDs = [];
//Remove --None--
for (var k= 0; k < rightOptions.length; k++) {
var value = rightOptions[k].innerHTML;
if (value == '--None--') {
rightBucket.remove(0);
}
}
// Add new options
if (arrName.length > 0) {
var myCIArray = arrName.toString().split(',');
for (var j = 0; j < myCIArray.length; j++) {
addOption(rightBucket, arrSysId[j], myCIArray[j]);
sortSelect(rightBucket);
leftBucket.onchange();
}
}
// sort the buckets
sortSelect(rightBucket);
}
Script Include:
var ConduentAJAX = Class.create();
ConduentAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getApprovers : function() {
var tmglist = [];
var bsparm = this.getParameter('sysparm_bs');
var parm = this.getParameter('sysparm_view');
var jsonArr = [];
var tgmusr = new GlideRecord('sys_user');
tgmusr.addQuery('sys_id', bsparm.u_tgms);
tgmusr.query();
while (tgmusr.next()) {
if(parm == 'portal'){
tmglist.push(tgmusr.name.toString());
}
else if(parm == 'native'){
var obj = {};
obj.name = tgmusr.name.toString();
obj.sys_id = tgmusr.name.toString();
jsonArr.push(obj);
}
}
if(parm == 'portal')
return tgmlist.toString();
else
return JSON.stringify(jsonArr);
},
type: 'ConduentAJAX'
});
Thanks again stumped on this one, I feel like I'm over complicating it.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 08:03 PM
Before I dig deeper into a solution, will your users be able to edit the list of approvers? If not, then I'd question why even show them. Let your Flow/Workflow get the approvers from the CI behind the scenes and not even show them to the user. Not much point showing someone something they can't change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 10:03 AM
Hi @Chad R
Please try following and let me know -
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
// Call the AJAX function to get the JSON data
var ajax = new GlideAjax('ConduentAJAX');
ajax.addParam('sysparm_name', 'getApprovers');
ajax.addParam('sysparm_bs', newValue);
ajax.getXML(populateValues);
}
function populateValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
var parsedData = JSON.parse(answer);
var listCollector = g_form.getControl('u_tgm_approver'); // Update with your field name
// Clear existing options
while (listCollector.options.length > 0) {
listCollector.remove(0);
}
// Add new options
for (var i = 0; i < parsedData.length; i++) {
var option = document.createElement('option');
option.value = parsedData[i].sys_id;
option.text = parsedData[i].name;
listCollector.appendChild(option);
}
}
}
Script Include:
var ConduentAJAX = Class.create();
ConduentAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getApprovers: function() {
var bsparm = this.getParameter('sysparm_bs');
var jsonArr = [];
// Query for your approvers based on the business service
var tgmusr = new GlideRecord('sys_user');
tgmusr.addQuery('sys_id', bsparm.u_tgms);
tgmusr.query();
while (tgmusr.next()) {
var obj = {
name: tgmusr.name.toString(),
sys_id: tgmusr.sys_id.toString()
};
jsonArr.push(obj);
}
return JSON.stringify(jsonArr);
},
type: 'ConduentAJAX'
});
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Regards,
Tushar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 11:18 AM - edited 08-29-2023 12:26 PM
Thanks for the reply @Tushar , still no luck, the value of the list collector just remains blank, I no longer get the glide ajax unhandled exception however.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 08:03 PM
Before I dig deeper into a solution, will your users be able to edit the list of approvers? If not, then I'd question why even show them. Let your Flow/Workflow get the approvers from the CI behind the scenes and not even show them to the user. Not much point showing someone something they can't change.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 09:15 AM
Thank you for this reality check Jim, your absolutely right I have no idea why I didn't question that before hand. I'm working on a solution that lets the flow take care of pulling those values from the business service and forget the variables all together. Thank you both for your replies I have no one else on the team I can bounce things off of and sometimes it helps to get to talk it out,