- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 03:29 AM
Having recently been working through
The requirement is to remove several options from a choice list (u_closed_code) on the Catalog Task table when the associated Requested Item (request_item) 'Item' (cat_item) is 'Quick Call'.
I created the following onLoad Client Script to try and achieve this:
function onLoad() {
function returnItem() { // declare the function
var reqItemType = g_form.getReference('request_item'); // get the Request Item
var closeChoices = []; // create an array to store the choices
var qcChoices = new GlideRecord('sys_choice');
qcChoices.addEncodedQuery('element=u_closed_code^labelLIKEQuick Call'); // query for our choices
qcChoices.query();
while (qcChoices.next()) {
closeChoices.push(qcChoices.getValue('value')); // when we find a matching choice, push the value field into the array
}
if (reqItemType.cat_item != '834a54a21bb1cdd0942bfc4cd34bcbe6' /* sys_id for Quick Call */) { // If the Request Item 'Item' IS NOT Quick Call
closeChoices.forEach(function(value) {
g_form.removeOption('u_closed_code', value); // for each item in the array, remove the option with the corresponding value
});
}
}
returnItem(); // call the function
}
Despite the code looking okay to my virgin eyes, it doesn't work. It seems to run fine in a background script, however, by inserting a few alerts into my code, it doesn't look like the while loop is running - it doesn't generate alerts while in the loop anyway. Any help would be appreciated, and\or tips on improving the code would be great, thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 08:17 AM
Hi,
addEncodedQuery() won't work in client side GlideRecord
update as this
function onLoad() {
function returnItem() { // declare the function
var reqItemType = g_form.getReference('request_item'); // get the Request Item
var closeChoices = []; // create an array to store the choices
var qcChoices = new GlideRecord('sys_choice');
qcChoices.addQuery('element','u_closed_code');
qcChoices.addQuery('label', 'LIKE', 'Quick Call');
qcChoices.query();
while (qcChoices.next()) {
closeChoices.push(qcChoices.getValue('value')); // when we find a matching choice, push the value field into the array
}
if (reqItemType.cat_item != '834a54a21bb1cdd0942bfc4cd34bcbe6' /* sys_id for Quick Call */) { // If the Request Item 'Item' IS NOT Quick Call
closeChoices.forEach(function(value) {
g_form.removeOption('u_closed_code', value); // for each item in the array, remove the option with the corresponding value
});
}
}
returnItem(); // call the function
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 03:40 AM
Hi,
Did you add alert to check the query is working fine?
is u_closed_code a field on sc_task?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 03:48 AM
Hi Ankur. As mentioned in my post; "by inserting a few alerts into my code, it doesn't look like the while loop is running - it doesn't generate alerts while in the loop anyway".
And yes, u_closed_code is on the sc_task table:
Although, I've now just realised it's an Integer field. I'll update my script to parseInt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 08:17 AM
Hi,
addEncodedQuery() won't work in client side GlideRecord
update as this
function onLoad() {
function returnItem() { // declare the function
var reqItemType = g_form.getReference('request_item'); // get the Request Item
var closeChoices = []; // create an array to store the choices
var qcChoices = new GlideRecord('sys_choice');
qcChoices.addQuery('element','u_closed_code');
qcChoices.addQuery('label', 'LIKE', 'Quick Call');
qcChoices.query();
while (qcChoices.next()) {
closeChoices.push(qcChoices.getValue('value')); // when we find a matching choice, push the value field into the array
}
if (reqItemType.cat_item != '834a54a21bb1cdd0942bfc4cd34bcbe6' /* sys_id for Quick Call */) { // If the Request Item 'Item' IS NOT Quick Call
closeChoices.forEach(function(value) {
g_form.removeOption('u_closed_code', value); // for each item in the array, remove the option with the corresponding value
});
}
}
returnItem(); // call the function
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-28-2022 08:58 AM
Thanks Ankur, I did not know that about encoded queries and client scripts. It works great now!