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

@Wayne Richmond 

Glad to help.

Please mark response helpful as well.

Regards
Ankur

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

Wayne Richmond
Tera Guru

Here is the final code for anyone who may find it useful:

function onLoad() {
    var sDesc = g_form.getValue('short_description').toLowerCase(); // store the value of Short Description in a variable and convert to lowercase
    if (sDesc.search('quick call') == -1) { // search Short Description for "quick call"
        returnItem(); // call the function if we don't find "quick call" so we can remove the options
    } else return; // if we find "quick call", exit the script, we don't need to remove anything

    function returnItem() { // declare the function
        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(parseInt(qcChoices.getValue('value'))); // when we find a matching choice, push the value field into the array while converting it to a number because the field type in an integer 
        }
        closeChoices.forEach(function(value) {
            g_form.removeOption('u_closed_code', value); // for each item in the array, remove the option with the corresponding value
        });
    }
}

SELECT Username
ServiceNow Employee
ServiceNow Employee

FYI, It's contrary to best practices to do a GlideRecord from client side so it should be avoided.  The reason for this is that it will hold up everything in the browser while it makes a call back to the instance, slowing form load (since your script appears to run onLoad).

I suggest pulling the data asynchronously via GlideAjax instead, which is consistent with best practices.  This will work out better if you run a HealthScan (fewer issues to resolve later) and it should improve client performance.  The latter is particularly true in the event that you're running multiple scripts on the same form.