Catalog item - how to have concatenation of 2 list collectors in a 3rd one?

vanessaheux
Tera Contributor

Hello

I have 2 list collectors in my catalog item based on same reference table and I would like to fill a third list collector with all values selected from the 2 first list collectors ?

I just need to update the "selected values" of the 2 first List collectors in the 3rd one.

Ex

current.variables.lc1 is the first list collector. User has selected values v1 and v2

current.variables.lc2 is the second one. User has selected values v1 and v3

I would like to dynamically fill the current.variables.lc3 which is the third list collector with all UNIQUE values selected in lc1 and lc2 (so v1, v2 and v3)

 

How to proceed?

Vanessa

1 ACCEPTED SOLUTION

something like this

this client script would be on both the list collectors

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

	var val1 = g_form.getValue('lc1').split(',');
	var val2 = g_form.getValue('lc2').split(',');

	// now merge both arrays and remove duplicates and set the value to 3rd one

	var finalArray = arrayUnique(val1.concat(val2));
	g_form.setValue('lc3', finalArray.toString());

}

function arrayUnique(array) {
	var a = array.concat();
	for(var i=0; i<a.length; ++i) {
		for(var j=i+1; j<a.length; ++j) {
			if(a[i] === a[j])
				a.splice(j--, 1);
		}
	}
	return a;
}

Regards
Ankur

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

View solution in original post

11 REPLIES 11

Yes I'm sure all 3 list collectors refer to the same table.

What comes in finalArray is correct by what I see in Alert.

It is just that the 3rd list collector is not  displayed correctly in my formular with the instruction 

	g_form.setValue('lc3', finalArray.toString());

Are you sure we can do g_form.setValue on list collectors?

There still must be something wrong in thsi statement.

Hi,

yes why not

Did you try setting single sys_id in that 3rd list collector?

Regards
Ankur

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

Yes I did.

then did it work?

If yes then total how many sysIds are there in final array?

Regards
Ankur

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

No it did not work.

The logs are correct and contain only 3 sys_ids (number in the final array for my test) but on the formular it was not correct.

I finally succeed to implement it by another way

function onLoad() {
//Type appropriate comment here, and begin script below

var varName11 = 'roles_to_add'; //"list1" is name of 1st list collector
var rightBucket11 = gel(varName11 + '_select_1');
var rightList11 = rightBucket11.options;

var varName12 = 'roles_to_add_by_transaction'; //"list1" is name of 1st list collector
var rightBucket12 = gel(varName12 + '_select_1');
var rightList12 = rightBucket12.options;

var varName13 = 'roles_to_add_by_template'; //"list1" is name of 1st list collector
var rightBucket13 = gel(varName13 + '_select_1');
var rightList13 = rightBucket13.options;

var varName2 = 'roles_to_add_final_list'; //"list2" is name of 2nd list collector
var rightBucket2 = gel(varName2 + '_select_1');
var rightList2 = rightBucket2.options ;

var selectedIDs11 = new Array();
for( var i = 0; i < rightList11.length; i++){
selectedIDs11[i] = rightList11[i].value;
}

var selectedIDs12 = new Array();
for( var j = 0; j < rightList12.length; j++){
selectedIDs12[j] = rightList12[j].value;
}

var selectedIDs13 = new Array();
for( var k = 0; k < rightList13.length; k++){
selectedIDs13[k] = rightList13[k].value;
}

removeListOptions(rightList11,rightBucket2); // remove none option
movetoList2(rightList11, selectedIDs11, rightBucket2);
movetoList2(rightList12, selectedIDs12, rightBucket2);
movetoList2(rightList13, selectedIDs13, rightBucket2);

// alert('Check Final roles to add');
//return false;
}


function movetoList2(rightList1,selectedIDs, rightBucket2){
for(var i = 0; i < rightList1.length; i++){
// alert(selectedIDs[i]);
var us = new GlideRecord('u_verallia_roles');
us.addQuery('sys_id', selectedIDs[i]);
us.query();
while (us.next()){
var option = document.createElement("option");
option.value = us.sys_id;
option.text = us.u_value;
rightBucket2.add(option);
}
}
}

function removeListOptions(rightList1,rightBucket2){
rightBucket2.length =0;
}