Remove Option on category not working in Onload client script

dagarson
Tera Guru

Hello I am trying to remove some category options when a case is assigned to a specific group. I was able to get a script working with a ui policy for on change but I need the same to work when you load the form. So I am an onload client script but it doesnt seem to remove the category options. All the alerts are running so I know the script is hitting but the options arent being removed. Here is the script

 

function onLoad() {
    var targetGroup = 'b5ca7f751b70529009c7202ce54bcb78'; 
    var currentGroup = g_form.getValue('assignment_group');

    // Proceed only if the current group matches the target group
    if (currentGroup === targetGroup) {
        alert("Target group matched.");

        // Alert current category value
        var currentCategoryValue = g_form.getValue('category');
        alert("Current category value: " + currentCategoryValue);

        // Remove categories one by one
        g_form.removeOption('category', '1000');
        g_form.removeOption('category', '1001');
        g_form.removeOption('category', '120000000000000');
        g_form.removeOption('category', '12311111');
        g_form.removeOption('category', '1233333332');
        g_form.removeOption('category', '21111111');
        g_form.removeOption('category', '211111111112');
        g_form.removeOption('category', '2222221111');
        g_form.removeOption('category', '222222211111114');
        g_form.removeOption('category', '222222221111');
        g_form.removeOption('category', '22222224');
        g_form.removeOption('category', '4030942586');
        g_form.removeOption('category', '4222211');
        g_form.removeOption('category', '43222222');
        g_form.removeOption('category', '455555621');
        g_form.removeOption('category', '54411225');
        g_form.removeOption('category', '544444444');
        g_form.removeOption('category', '563842034');
        g_form.removeOption('category', '57391921111');
        g_form.removeOption('category', '600');
        g_form.removeOption('category', '601');
        g_form.removeOption('category', '602');
        g_form.removeOption('category', '603');
        g_form.removeOption('category', '604');
        g_form.removeOption('category', '605');
        g_form.removeOption('category', '6051');
        g_form.removeOption('category', '606');
        g_form.removeOption('category', '607');
        g_form.removeOption('category', '608');
        g_form.removeOption('category', '609');
        g_form.removeOption('category', '6091');
        g_form.removeOption('category', '610');
        g_form.removeOption('category', '611');
        g_form.removeOption('category', '6111');
        g_form.removeOption('category', '6112');
        g_form.removeOption('category', '6113');
        g_form.removeOption('category', '612');
        g_form.removeOption('category', '613');
        g_form.removeOption('category', '614');
        g_form.removeOption('category', '615');
        g_form.removeOption('category', '616');
        g_form.removeOption('category', '617');
        g_form.removeOption('category', '618');
        g_form.removeOption('category', '619');
        g_form.removeOption('category', '620');
        g_form.removeOption('category', '621');
        g_form.removeOption('category', '622');
        g_form.removeOption('category', '623');
        g_form.removeOption('category', '624');
        g_form.removeOption('category', '625');
        g_form.removeOption('category', '626');
        g_form.removeOption('category', '627');
        g_form.removeOption('category', '644443333');
        g_form.removeOption('category', '69045032');
        g_form.removeOption('category', '701');
        g_form.removeOption('category', '702');
        g_form.removeOption('category', '703');
        g_form.removeOption('category', '704');
        g_form.removeOption('category', '705');
        g_form.removeOption('category', '706');
        g_form.removeOption('category', '707');
        g_form.removeOption('category', '708');
        g_form.removeOption('category', '73222111');
        g_form.removeOption('category', '7435945');
        g_form.removeOption('category', '750');
        g_form.removeOption('category', '751');
        g_form.removeOption('category', '752');
        g_form.removeOption('category', '753');
        g_form.removeOption('category', '754');
        g_form.removeOption('category', '755');
        g_form.removeOption('category', '756');
        g_form.removeOption('category', '757');
        g_form.removeOption('category', '758');
        g_form.removeOption('category', '759');
        g_form.removeOption('category', '7845555');
        g_form.removeOption('category', '785555555');
        g_form.removeOption('category', '78888888');
        g_form.removeOption('category', '7950320324');
        g_form.removeOption('category', '8655766');
        g_form.removeOption('category', '877776677');
        g_form.removeOption('category', '9322222');

        alert("Categories have been removed.");
    }
}

Any assistance would be helpful.

1 ACCEPTED SOLUTION

dagarson
Tera Guru

I was able to get this resolved. by using the setTimeout function along with clearOption I was able to clear every option and only add the ones I wanted.

View solution in original post

7 REPLIES 7

-O-
Kilo Patron
Kilo Patron

I would edit the dictionary for category, making it dependent on assignment_group and adding attribute choice_script to the former.

Something like: choice_script=new SomeScriptInclude().someMethodReturningChoices().

Of course you would need to define Script Include SomeScriptInclude. In its someMethodReturningChoices (which you also would define) you could read the selected group from global variable dependent_value.

Than you could use the dependent_value field on the choice records to configure which category to be included or excluded without ever touching the script again.

To sweeten the deal, this will work and will be honored in all UIs.

-O-
Kilo Patron
Kilo Patron

This is how the Script Include could look like:

var SomeScriptInclude = Class.create();
 
SomeScriptInclude.prototype = {
 
	'initialize': function() {
 		this._dependent_value = typeof dependent_value != 'undefined' ? dependent_value : undefined);
	},
 
	'someMethodReturningChoices': function(dictionary) {
		var choiceList = new GlideChoiceList();
		
		// Load choice by GlideRecord using this._dependent_value in the query and adding the choices as below:
		choiceList.add(new GlideChoice(<choice value>, <choice label>));

		return choiceList;
	},

	'type': 'SomeScriptInclude',
}

dagarson
Tera Guru

I was able to get this resolved. by using the setTimeout function along with clearOption I was able to clear every option and only add the ones I wanted.