Remove selected option List collector

Brian Lancaster
Tera Sage

I have a filter on my list collector that is based on the select box.  If someone changes the select box I need to remove all selected option from the list collector as they will not longer be valid.  How can I achieve this?  I need a solution that works in Service Portal as well as standard ServiceNow.

1 ACCEPTED SOLUTION

It was the last link that gave me how to do it in Service Portal.  Here is the code for it to work in both service portal and ServiceNow.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var collectorName = 'ad_groups';
	//Type appropriate comment here, and begin script below
	try {
		var myListCollector = g_list.get(collectorName);
		var selectedOptions = g_form.getValue(collectorName).split(',');
		if((selectedOptions != "") && (selectedOptions.length > 0)){
			//Remove items
			var index = 0;		
			for (var i = 1, leng = selectedOptions.length+1; i < leng; i++){
				myListCollector.removeItem(selectedOptions[selectedOptions.length-i]);
				index++;
			}
		}
	}
	
	catch(e){
		//Name of variable to move options from
		var varName = collectorName;
		var leftBucket = gel(varName + '_select_0');
		var rightBucket = gel(varName + '_select_1');
		var selectedOptions = rightBucket.options;
		
		//Get an array of all option IDs to move
		var selectedIDs = new Array();
		var index = 0;
		for(var i = 0; i < selectedOptions.length; i++){
			selectedIDs[index] = i;
			index++;
		}
		
		//Move all returned options from right to left bucket and sort the results
		//Switch 'rightBucket' and 'leftBucket' to move from left to right
		moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
		//Sort the resultant options in the left bucket
		sortSelect(leftBucket);
	}
}

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Bricast,

Following links should be helpful:

https://community.servicenow.com/community?id=community_blog&sys_id=571da2e5dbd0dbc01dcaf3231f961913

https://community.servicenow.com/community?id=community_blog&sys_id=524d6229dbd0dbc01dcaf3231f961903

https://community.servicenow.com/community?id=community_question&sys_id=d0be72a5db58dbc01dcaf3231f96194a

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

It was the last link that gave me how to do it in Service Portal.  Here is the code for it to work in both service portal and ServiceNow.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var collectorName = 'ad_groups';
	//Type appropriate comment here, and begin script below
	try {
		var myListCollector = g_list.get(collectorName);
		var selectedOptions = g_form.getValue(collectorName).split(',');
		if((selectedOptions != "") && (selectedOptions.length > 0)){
			//Remove items
			var index = 0;		
			for (var i = 1, leng = selectedOptions.length+1; i < leng; i++){
				myListCollector.removeItem(selectedOptions[selectedOptions.length-i]);
				index++;
			}
		}
	}
	
	catch(e){
		//Name of variable to move options from
		var varName = collectorName;
		var leftBucket = gel(varName + '_select_0');
		var rightBucket = gel(varName + '_select_1');
		var selectedOptions = rightBucket.options;
		
		//Get an array of all option IDs to move
		var selectedIDs = new Array();
		var index = 0;
		for(var i = 0; i < selectedOptions.length; i++){
			selectedIDs[index] = i;
			index++;
		}
		
		//Move all returned options from right to left bucket and sort the results
		//Switch 'rightBucket' and 'leftBucket' to move from left to right
		moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
		//Sort the resultant options in the left bucket
		sortSelect(leftBucket);
	}
}

Hi Bricast,

So is it resolved now? If yes then can you mark the answer as correct, helpful. Thanks in advance.

Regards

Ankur

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