The CreatorCon Call for Content is officially open! Get started here.

Auto select values based in list collector base on another variable..

KaMmILa__
Tera Expert

We have a multiple choice variable with choices A, B, C and a list collector which is referred to a table and the list in list collector are 1,2,3,4,5,6,7,8 

My requirement is when the user select Option A, then in the list collector should auto select 1,2,3 and make them read-only as we don't want the user to remove those

can we remove the 1,2,3 from the left panel of the list collector?

and the user may need to select the other values too(4,5,6,7,8) 

 

, I came across this link and tried implementing this but stuck 

https://www.servicenowguru.com/scripting/client-scripts-scripting/move-list-collector-options/

 

I created an on change client script based on the above link and selected the option A where exactly I need to set 1,2 3, values ?

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
if(newValue=="A")
	{
		//Name of variable to move options from
var varName = 'csv_netbadge_int_ldap_attributes1';
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(rightBucket);
	}
   //Type appropriate comment here, and begin script below
   
}
1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

This requirement can be done using below steps:

 

  1. Create one onChange catalog client based on choice value, where you will filter those auto populate value from list collector and then user will see rest of other values to select and add it manually from left to right bucket. 
  2. Create onSubmit Client to set those auto populate value while submitting the form request, you have to use here glide ajax and client callable script include 

 

Sample Code:

 

First onChange catalog client script

 

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


	var collectorName = 'u_ldap'; // your list collector name
	var filterString = '';
	if(newValue == 'Option A')
		filterString='name!=Test 1^ORname=NULL^name!=Test 2^ORname=NULL^name!=Test 3';
	else if(newValue == 'Option B')
		filterString='name!=Test 3^ORname=NULL^name!=Test 4^ORname=NULL';

	//Reset the filter query
	window[collectorName + 'g_filter'].reset();
	window[collectorName + 'g_filter'].setQuery(filterString);
	window[collectorName + 'acRequest'](null);

}

 

onSubmit Catalog Client script using glide ajax :

 

function onSubmit() {
	//Type appropriate comment here, and begin script below
	// 	var newValue = g_form.getValue('choose_option_here');

	var ajax = new GlideAjax('checkSubmit');
	ajax.addParam('sysparm_name', 'checkgp');
	ajax.addParam('sysparm_name_choice', g_form.getValue('choose_option_here'));
	ajax.getXML(populateValues);

	function populateValues(response) {
		var answer = response.responseXML.documentElement.getAttribute("answer");

		//Name of variable to move options from
		var varName = 'u_ldap';
		var leftBucket = gel(varName + '_select_0');
		var rightBucket = gel(varName + '_select_1');

		var res = JSON.parse(answer);

		for(var i= 0; i< res.length ; i ++){

			var option = document.createElement("option");

			option.value =  res[i].id;
			option.text = res[i].name;

			rightBucket.add(option);
		}

		sortSelect(rightBucket);

	}


	return false; // to test i set it so you can see the value
}

 

Script Include:

 

var checkSubmit = Class.create();
checkSubmit.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkgp: function(){

		var parm = this.getParameter('sysparm_name_choice');
		var qr='';
		if(parm == 'Option A'){
			qr +='name=Test 1^ORname=Test 2^ORname=Test 3';
		}
		else if(parm == 'Option B'){
			qr +='name=Test 3^ORname=Test 4';
		}
		var arr = [];
		var gr= new GlideRecord('cmdb_ci_endpoint_ldap');
		gr.addEncodedQuery(qr);
		gr.query();
		while (gr.next()) {
			var obj={};

			obj.name = gr.getValue('name');
			obj.id = gr.getUniqueValue();
			arr.push(obj);
		}
		var json = new JSON().encode(arr);
		return json;


	},

	type: 'checkSubmit'
});

 

Hope it will help 

 

Note: This way you can restrict user to not remove the autopopulated values in list collector. 

View solution in original post

14 REPLIES 14

Brad Bowman
Kilo Patron
Kilo Patron

When Option A is selected, you can move 1,2,3 from the left to the right, but you can't make them read-only or else no other selections could be made.  What you can do is create an onChange Catalog Client Script on the list collector variable that ensures 1,2, and 3 are always on the right when Option A is selected, etc.

First here is what the onChange Catalog Client Script for the multiple choice variable will look like.  Make sure the Isolate script box is unchecked.

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
  return;
 }
	
 var varName = 'listvariable';//replace with the list collector variablename
 var leftBucket = gel(varName + '_select_0');
 var rightBucket = gel(varName + '_select_1');
 var selectedOptions = rightBucket.options;
 var selectedIDs = new Array();
 var index = 0;
 //first select anything/everything in the right bucket and move it to the left
 //this will ensure data integrity if the selected Option changes from A to B, etc
 for(var i = 0; i < selectedOptions.length; i++){
  selectedIDs[index] = i;
  index++;
 }
 moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');
 sortSelect(leftBucket);

 //now move 1,2, and 3 if Option A is selected, etc.
 var leftBucket2 = gel(varName + '_select_0');
 var rightBucket2 = gel(varName + '_select_1');
 var selectedOptions2 = leftBucket2.options;
 var selectedIDs2 = new Array();
 var index2 = 0;
 for(var j = 0; j < selectedOptions2.length; j++){
  if(newValue == 'A'){
   if(selectedOptions2[j].label == '1' || selectedOptions2[j].label == '2' || selectedOptions2[j].label == '3'){
    selectedIDs2[index2] = j;
    index2++;
   }
  }
  //repeat the 2 if blocks inside the for loop if Option B, then select 4,5,6 - or whatever your requirements are...
 }
 rightBucket2.options.length = '0';
 moveSelectedOptions(selectedIDs2, leftBucket2, rightBucket2);
 sortSelect(rightBucket2);
}

Here's what the onChange Catalog Client Script on the list collector variable would look like - also ensure the Isolate script box is unchecked.

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

 var varName = 'listvariable';//replace with the list collector variablename
 var leftBucket = gel(varName + '_select_0');
 var rightBucket = gel(varName + '_select_1');
 var selectedOptions = leftBucket.options;
 var selectedIDs = new Array();
 var index = 0;
 for(var i = 0; i < selectedOptions.length; i++){
  if(g_form.getValue('multiple_choice') == 'A'){//replace with multiple choice variable name and choice Value
   if(selectedOptions[i].label == '1' || selectedOptions[i].label == '2' || selectedOptions[i].label == '3'){
    selectedIDs[index] = i;
    index++;
   }
   moveSelectedOptions(selectedIDs, leftBucket, rightBucket);
   sortSelect(rightBucket);
  }
  //repeat the 2 if blocks within the for loop if Option B, then select 4,5,6 - or whatever your requirements are...//
 }
}

 

KaMmILa__
Tera Expert

Thanks, Brad, I tried the above code ( we may not need the read-only part but if the option is A in the multiple choice then we need to pre-select the values 1,2,3 from left to right. if option B then 4,5,6 but we also need to give the user to select the other values too.

when using the above code (only the client script on multiple-choice  then its not populating the values in the list collector instead showing as loading.. 

I replaced the with list collector 

 var varName = 'listvariable';//replace with the list collector variablename

replaced the correct value and in 1,2,3 replaced with proper values , sill not showing up 

 if(newValue == 'A');
   if(selectedOptions2[j].value == '1' || selectedOptions2[j].value == '2' || selectedOptions2[j].value == '3'){
    

find_real_file.png

To move record from left to right, they must first exist in the Available/left side.  The selections must also be in the first 50, or whatever your glide.xmlhttp.excessive system property is set to as the max number of records loaded into a slushbucket.  The script I suggested runs onChange and doesn't affect the filter/available records.  When this form loads are all of the records shown in the Available list?  Do you have a dynamic reference qualifier on the list collector variable, or an onChange script that is re-filtering the available records based on the multiple choice selection?

Thanks Brad

there are only 12 records in the list collector, I created the on change client script it self and there are no filters in the list collector, there is no other client scripts