Clear list collector selected values when some field is changed in Service Portal AND Desktop

Baggies
Kilo Guru

Clear list collector selected values when some field is changed in Service Portal.

 When I change the value of one variable, a choice list, on a catalog item, I want the values from a list collector all cleared out. I have managed to achieve this with help from this forum. Trouble is, I have had to create  one script for the portal, and one for desktop view. here they are list below. Can I achieve the same results with one script only which will work in Portal  and desktop views? "add_user_to_group" is my list collector name. Many thanks. Mark S.

 

Portal script:

Works in portal
Name:
Clear values on change
Type: onChange
UI Type: Mobile/Portal *** THIS IS IMPORTANT *****

----------------------------------------------------------------------------------------------------------------------------------------------------------

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var collectorName = 'add_user_to_group'; //Name of your list collector.
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++;
alert(" ** changed in portal ***");
}
}

}

--------------------------------------------------------------------------------------------------------------

Works in classic view
Name:
Clear values on change
Type: onChange
UI Type: Desktop *** THIS IS IMPORTANT *****


function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
var listCollectorName = 'add_user_to_group'; //Name of your list collector.

var leftBucket = gel(listCollectorName + '_select_0');
var rightBucket = gel(listCollectorName + '_select_1');

var selectedOptions = rightBucket.options;
var selectedIDs = [];
for(var i = 0; i < selectedOptions.length; i++){

//Check for the item to add here based on sys_id
selectedIDs.push(i);
//var value = selectedOptions[i].value;
//rightBucket.remove(value);
// var lefBucket = gel(listCollectorName + '_select_0');
//leftBucket.add(value);

}

moveSelectedOptions(selectedIDs,rightBucket,leftBucket,'--None--');
sortSelect(leftBucket);
var leftOptions = leftBucket.options;
for(var j = 0; j<leftOptions.length; j++){
var value = leftOptions[j].innerHTML;
if(value == '--None--'){

leftBucket.remove(j);
alert(" ** changed in CLASSIC view ***");

}
}
}

 

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

Change type to all and use below script to combine it.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	try{
		var collectorName = 'add_user_to_group'; //Name of your list collector.
		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++;
				alert(" ** changed in portal ***");
			}
		}
	}
	catch(err){
		//Type appropriate comment here, and begin script below
		var listCollectorName = 'add_user_to_group'; //Name of your list collector.
		var leftBucket = gel(listCollectorName + '_select_0');
		var rightBucket = gel(listCollectorName + '_select_1');
		var selectedOptions = rightBucket.options;
		var selectedIDs = [];
		for(var i = 0; i < selectedOptions.length; i++){
			//Check for the item to add here based on sys_id
			selectedIDs.push(i);
			//var value = selectedOptions[i].value;
			//rightBucket.remove(value);
			// var lefBucket = gel(listCollectorName + '_select_0');
			//leftBucket.add(value);
		}
		moveSelectedOptions(selectedIDs,rightBucket,leftBucket,'--None--');
		sortSelect(leftBucket);
		var leftOptions = leftBucket.options;
		for(var j = 0; j<leftOptions.length; j++){
			var value = leftOptions[j].innerHTML;
			if(value == '--None--'){
				leftBucket.remove(j);
				alert(" ** changed in CLASSIC view ***");

			}
		}
	}
}

View solution in original post

5 REPLIES 5

thomas_wright1
Giga Contributor

Mike, 

This is interesting but wondering if how I have my variable list collector setup causes this not to work either way. 

I have a catalog item that displays in a UI page.  I have a variable set that contains a list collector. If I set the UI type on the variable, i and use that code I get a console error of g_list is not defined.  If I change the UI type to desktop, I get gel is not defined.  

Is there something special that I need to do on the UI page side so that it will know what either one is? 

 

Thanks

Tom