Populate Email Addresses of users selected in List Collector variable to Multi Line Text Variable

Vinay52
Tera Expert

Hello, Requesters would be selecting the users using List Collector and requirement is to copy the email addresses of selected users with comma separated (Right Side of List collector) to another variable (Multi Line Text Variable). Text Variable to make it read only and visible to Fulfillers. 

I have tried creating script by looking some past examples but seems its not working. Any suggestions on what seems to be wrong please

Variable Details: find_real_file.png

Client Script 

Type - onChange, Variable Name - select_users  //list collector variable 

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

  if(newValue === '') {
		g_form.clearValue('copied_from_list');
	}

	var gaPhone = new GlideAjax('getEmailAddress');
	gaPhone.addParam('sysparm_name', 'get_email');
	gaPhone.addParam('sysparm_users', newValue);
	gaPhone.getXMLAnswer(_handleResponse);

	function _handleResponse(response) {
		var answer = response;
		
		g_form.setValue('copied_from_list', answer);
	}
  
}

Script Include - Client Callable: Checked

var getEmailAddress = Class.create();
getEmailAddress.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	get_email : function() {
		var userArr = this.getParameter('sysparm_users').split(',');
		var emailArr = [];

		for(var i = 0; i < userArr.length; i++) {
			var grUser = new GlideRecord('sys_user');

			if(grUser.get(userArr[i])) {
				emailArr.push(grUser.getValue('email'));
			}
		}
		emailArr.join();
		
		return emailArr.toString();
	},
		
    type: 'getEmailAddress'
	
});

 

1 ACCEPTED SOLUTION

Hi Vinay,

then you can use Run Script in the workflow of catalog item

Updated: to remove current.update()

var userArr = current.variables.select_users.toString().split(',');
var emailArr = [];

for(var i = 0; i < userArr.length; i++) {
	var grUser = new GlideRecord('sys_user');

	if(grUser.get(userArr[i].toString())) {
		emailArr.push(grUser.getValue('email'));
	}
}

var values = emailArr.join();
current.variables.copied_from_list = values;

Regards
Ankur

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

View solution in original post

16 REPLIES 16

asifnoor
Kilo Patron

Hi,

Try this code.

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

	var gaPhone = new GlideAjax('getEmailAddress');
	gaPhone.addParam('sysparm_name', 'get_email');
	gaPhone.addParam('sysparm_users', newValue);
	gaPhone.getXMLAnswer(_handleResponse);

	function _handleResponse(response) {
		alert(response);
		g_form.setValue('copied_from_list', response);
	}
  
}

Script Include

var getEmailAddress = Class.create();
getEmailAddress.prototype = Object.extendsObject(AbstractAjaxProcessor, {	
	get_email : function() {
		var userArr = this.getParameter('sysparm_users').toString().split(',');
		var emailArr = [];

		for(var i = 0; i < userArr.length; i++) {
			var grUser = new GlideRecord('sys_user');

			if(grUser.get(userArr[i].toString())) {
				emailArr.push(grUser.getValue('email'));
			}
		}
		emailArr.join();
		
		return emailArr.toString();
	},
		
    type: 'getEmailAddress'
	
});

Mark the comment as a correct answer and helpful if this helps to solve the problem.

Community Alums
Not applicable

Thanks for this code. This is working super fine.

** to show the email address in the text variable, just write down one line code after alert in client script.

 

g_form.setValue('business_owner_email', response);

Where ('business_owner_email') is my variable field name.

Vinay52
Tera Expert

Hello, Thank you both for your response. Checked with alert and response seems to be coming from Script Include, If i select and add more than 5 user records in list collector and submit the form in Service Portal, can see the values in text variable. 

If right side selected user list is less than 5 then text variable showing empty after submission and then if i select and try to add any user then i get response from script include. 

find_real_file.pngfind_real_file.png

Try below to run in synchronous mode.

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

  if(newValue === '') {
		g_form.clearValue('copied_from_list');
	}

	var gaPhone = new GlideAjax('getEmailAddress');
	gaPhone.addParam('sysparm_name', 'get_email');
	gaPhone.addParam('sysparm_users', newValue);
	gaPhone.getXMLAnswer(handleResponsefn);

	function handleResponsefn(ans) {
		var answer = ans;		
		g_form.setValue('copied_from_list', answer);
	}
  
}

Can you let us know why you are copying the email addresses in a multi line text field. Is this field should be visible while submitting the form? or if you want to show this field for fulfillers then the best way is to update the variables in workflow.

Yes, It should be visible to only fulfillers as read only to copy all the email addresses.

This shouldn't be visible while submitting. 

I have tried to run in synchronous mode but it does't copy the email addresses if its less than 5 records.