Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Get List Collector selections into field on form

Marcel H_
Tera Guru

I'm hoping someone can help me out with an issue that I'm having that I can't seem to get working correctly. I've read a lot of posts here but nothing has quite worked the way that I'm hoping it will.

I have a new Service Catalog offering that I've set up, and on that offering I have a variable that is a List Collector pointing to a custom table, u_external_user. This table is used to hold individuals like visitors, vendor personnel, people coming to interview, etc. and is referenced by other apps in the system that we have for visitor management, NDAs, etc. The list collector would allow selections from this table, or if a record for the person doesn't exist, a button will allow for a UI Page popup to add a new record to the table and automatically add the new entry to the right hand slush bucket.

Upon submitting the catalog item though, I would like to be able to then take the people selected in the list collector and push that information to a string field on the form, but also get other information about them from the table as well. If possible, having the field formatted for readability would be great. As an example, if John Smith is selected in the list collector, in the string field I'd like to display his name, company, email address and phone number (as pulled from the table) and then display it like:

John Smith, Company Name, email address, phone number

If there is more than one person selected, I'd like to see a line break between each person's information.

So far the examples I've seen here don't seem to work for me in testing. I've tried to just dump an array to a field like Description, but on submit nothing appears there. Any help is greatly appreciated.

1 ACCEPTED SOLUTION

I think you best option is to create a multi-line text variable on the item and only display it on the task level.  Then in your workflow use a run script will the following code.

var list = current.variables.List collector variable name.toString.split(',');

for (i = 0, i < list.length, i++){

var gr = new GlideRecord ('table name');

gr.addquery('sys_id', list[i]);

gr.query();

if (gr.next()){

current.variables.multi-line variable name += gr.u_name + ", " + gr.u_company_name + ', ' + gr.u_email_address + ', ' + gr.u_phone_number + '\n';

}

in the above script in the if section you would have to use you filed names from the table.  I just took a guess.

View solution in original post

6 REPLIES 6

How to do the reverse process of this,

If I am having multiple user names(comma separated) in multi line field, how to auto populate those names in list collector field?

 

Marcel H_
Tera Guru

Thanks for the help! It got me pointed in the right direction, which ended up as the code below:

var list = producer.employees_subcontractors.toString().split(','); //Split string on commas into an array.
var emp = ''; // Variable to hold employee/subcontractor list content
 for (i = 0; i < list.length; i++) {   // Iterate through user array
var gr = new GlideRecord('u_external_user');
   gr.addQuery('sys_id', list[i]);
   gr.query();
   if (gr.next()) {
  emp += gr.u_name + ", Company: " + gr.u_company.getDisplayValue() + ", Email: " + gr.u_email + ", Phone: " + gr.u_phone + ", Business phone: " + gr.u_work_phone + ", Mobile phone: " + gr.u_mobile_phone + ", Alias/Preferred name: " + gr.u_alias + '\n\n'; // Write content to variable
   }
}
current.u_employees_subcontractors = emp; // Set Employees/subcontractors to variable