How to fetch values from list collector in client script

Snehal13
Kilo Sage

How to fetch values from list collector in client script

1 ACCEPTED SOLUTION

RAMANA MURTHY G
Mega Sage
Mega Sage

@Snehal13 

ListCollector_Demo.png

 

I have tried with List Collector in Catalog Client Script to fetch the email ID of users and shows in the text field below

Here is the code (you can change it according to your requirements)

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var traineesNames = g_form.getValue('select_trainee');  // 'select_trainee' is the backend name of the List Collector
	var trainees = traineesNames.split(',');
	var traineeEmails = [];
	
	for(var i=0;i<trainees.length;i++){
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',trainees[i]);
		gr.query();
		while(gr.next()){
			traineeEmails.push(gr.email);
		}
	}
	g_form.setValue('trainees_email_id',traineeEmails);  // 'trainees_email_id' is the back end name of the text field
   
}

 

 

Please give me correct and helpful, if my answer helps to your requirement.

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

7 REPLIES 7

Prince Arora
Tera Sage
Tera Sage

@Snehal13 

 

You can use "g_form.getValue('BACKEND_NAME_OF_LIST_COLLECTOR')"

 

It will return the sys_ids of selected items.

 

If my answer solved your issue, please mark my answer as  Correct & 👍Helpful based on the Impact.

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Snehal13 ,

 

generally list collector will return sys_id with comma seprated values. You can fetch the values from the list collector field by using g_form.getValue(name_of_field_here); > this will return the comma seprated sys_id which you can pass it to server side and perform operations on those sys_id.

 

I guess you question is incomplete can you please share the complete requitement to know what your trying to achive ?

 

i hope this helps, 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

kaushalmore
Tera Contributor


var listCollectorValue = g_form.getValue('list_collector_field_name');

if (listCollectorValue) {

var sysIds = listCollectorValue.split(',');


var displayValues = [];

// Loop through each sys_id and fetch its display value
for (var i = 0; i < sysIds.length; i++) {
var sysId = sysIds[i].trim(); // Trim to remove any extra spaces


var displayValue = g_list.getDisplayValue(sysId);

if (displayValue) {
displayValues.push(displayValue);
}
}


if (displayValues.length > 0) {


var displayString = displayValues.join(', ');

}
}
})();

mark helpful if it helps to solve your problem 

thank you 

 

RAMANA MURTHY G
Mega Sage
Mega Sage

@Snehal13 

ListCollector_Demo.png

 

I have tried with List Collector in Catalog Client Script to fetch the email ID of users and shows in the text field below

Here is the code (you can change it according to your requirements)

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var traineesNames = g_form.getValue('select_trainee');  // 'select_trainee' is the backend name of the List Collector
	var trainees = traineesNames.split(',');
	var traineeEmails = [];
	
	for(var i=0;i<trainees.length;i++){
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',trainees[i]);
		gr.query();
		while(gr.next()){
			traineeEmails.push(gr.email);
		}
	}
	g_form.setValue('trainees_email_id',traineeEmails);  // 'trainees_email_id' is the back end name of the text field
   
}

 

 

Please give me correct and helpful, if my answer helps to your requirement.

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer