Automatically fill fields from sys_user

Marielle Beset
Tera Contributor

I want to create a catalog item where multiple users can be selected, and fields are automatically filled with the user_name and email fields.

My screen should look like this:

 

Name from table sys_user | Automatically fill with user_name | Automatically fill with email

 

Is this easy to make, and if so, can someone explain it to me?

4 REPLIES 4

Omender Singh
Tera Guru

Hi @Marielle Beset ,

 

To auto-populate fields like user_name and email from the sys_user table in a ServiceNow catalog item when multiple users are selected, you can follow these steps:


1. Use a List Collector Variable to Select Multiple Users

  • Create a List Collector Variable:

    • Set the variable type to List Collector.

    • Reference the User [sys_user] table.

    • This allows selection of multiple users.

  • Configure Display Columns:

    • Use the variable attribute ref_ac_columns=user_name,email to display user_name and email in the list.

    • This enhances the user selection experience.


2. Auto-Populate user_name and email Fields

  • Create Additional Variables:

    • Add variables to your catalog item to hold the user_name and email values.

    • These can be of type Single Line Text or Multi Line Text, depending on your preference.

  • Use a Catalog Client Script:

    • Write an onChange client script for the List Collector variable.

    • In the script, use g_form.getReference() to retrieve the selected users' records.

    • Extract the user_name and email from each record and populate the corresponding variables.

    Example Script:

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

  var userIDs = g_form.getValue('user_list'); // Replace 'user_list' with your List Collector variable name
  var userIDArray = userIDs.split(',');

  var userNames = [];
  var emails = [];

  userIDArray.forEach(function(userID) {
    g_form.getReference(userID, function(user) {
      userNames.push(user.user_name);
      emails.push(user.email);

      // After processing all users, set the values
      if (userNames.length === userIDArray.length) {
        g_form.setValue('user_names', userNames.join(', ')); // Replace 'user_names' with your variable name
        g_form.setValue('emails', emails.join(', ')); // Replace 'emails' with your variable name
      }
    });
  });
}

 

This script retrieves the user_name and email for each selected user and populates the respective variables.

 

3. Alternative: Use GlideAjax for Server-Side Processing

  • Create a Script Include:

    • Define a Script Include that accepts user IDs and returns their user_name and email.

    •  

    Example Script Include:

var GetUserDetails = Class.create();
GetUserDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getDetails: function() {
    var userIDs = this.getParameter('sysparm_user_ids').split(',');
    var result = [];

    var gr = new GlideRecord('sys_user');
    gr.addQuery('sys_id', 'IN', userIDs);
    gr.query();

    while (gr.next()) {
      result.push({
        user_name: gr.user_name.toString(),
        email: gr.email.toString()
      });
    }

    return JSON.stringify(result);
  }
});

 

Modify the Client Script:

  • Use GlideAjax to call the Script Include and retrieve user details.

Example Client Script:

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

  var ga = new GlideAjax('GetUserDetails');
  ga.addParam('sysparm_name', 'getDetails');
  ga.addParam('sysparm_user_ids', newValue);
  ga.getXMLAnswer(function(response) {
    var users = JSON.parse(response);
    var userNames = users.map(function(user) { return user.user_name; });
    var emails = users.map(function(user) { return user.email; });

    g_form.setValue('user_names', userNames.join(', ')); // Replace with your variable name
    g_form.setValue('emails', emails.join(', ')); // Replace with your variable name
  });
}

By following these steps, you can create a catalog item that allows multiple user selections and automatically populates their user_name and email fields.

Ankur Bawiskar
Tera Patron
Tera Patron

@Marielle Beset 

you can have a list collector type variable referring to sys_user

Then write onChange catalog client script + GlideAjax on that and fill the name and email in some multi-line text variable

sample working script here; enhance for your case

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

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Marielle Beset 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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