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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you want to auto-populate the 3rd list collector right

then you will require 2 onChange client scripts each on 1 list collector

concatenate and then set the value in 3rd

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
	
}

Regards
Ankur

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

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

Hello Ankur

I have implemented what you proposed me but unfortunately, this doesn't work.

finalArray contains the list of sys_id of both list collectors but the list collector does not get updated correctly by instruction 

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

Nothing can be seen in the catalog item view.

Hi,

So the logic I shared of merging and getting unique is working fine?

are you sure all 3 list collectors refer to same table?

Did you add alert and check what came in finalArray?

Regards
Ankur

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