Client Script to remove options from choice list not working

Wayne Richmond
Tera Guru

Having recently been working through @Chuck Tomasi 's excellent 'Learn Javascript on the Now Platform' series on YT, I decided to put my new found coding skills to the test. 

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.  

find_real_file.png

find_real_file.png

find_real_file.png

 

1 ACCEPTED SOLUTION

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Did you add alert to check the query is working fine?

is u_closed_code a field on sc_task?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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:

find_real_file.png

Although, I've now just realised it's an Integer field. I'll update my script to parseInt

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks Ankur, I did not know that about encoded queries and client scripts. It works great now!