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

Hi Vinay,

Did you accidentally mark other response as correct.

Would you mind marking my response as correct if I was able to help/guide you.

Regards
Ankur

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

Both the responses were helpful. I did modified and removed the current.update from Run Script step from workflow. Thanks both. I have modified and made one answer helpful and other one correct. Thanks both.