We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Prepopulate assets for user

BrianS344076034
Tera Guru

I am redoing our exit alert and have the following in place right now

 

Computer(s) assigned

This looks at the cmdb_ci_computer table for assets assigned to the user and is a list collector.

What I want to know, is it possible that I just autopopulate that list with what is already known so the manager doesn't have to click the box and select items?

1 ACCEPTED SOLUTION

Itallo Brandão
Tera Guru

Hi @BrianS344076034 ,

Yes, absolutely! This is a great User Experience improvement. You can automatically select the items in the List Collector so the manager just has to "Review" them instead of searching for them.

Since the List Collector expects a comma-separated string of Sys_IDs to set the values, you need a Client Script (to handle the form) and a Script Include (to query the database).

Here is the recipe:

Step 1: Client Callable Script Include

Create a new Script Include.

  • Name: CatalogAssetHelper

  • Client callable: Checked (True)

 
var CatalogAssetHelper = Class.create();
CatalogAssetHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserComputers: function() {
        var userId = this.getParameter('sysparm_user_id');
        var computerIds = [];

        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('assigned_to', userId);
        // Optional: Add status query (e.g., only 'In Use')
        // gr.addQuery('install_status', 1); 
        gr.query();

        while (gr.next()) {
            computerIds.push(gr.getUniqueValue());
        }

        // Return a comma-separated string of SysIDs
        return computerIds.join(',');
    },

    type: 'CatalogAssetHelper'
});

Step 2: Catalog Client Script

Create a script on your Catalog Item.

  • Type: onChange

  • Variable name: The variable where you select the User (e.g., leaver or requested_for).

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

    // Call the server to get assets for this user
    var ga = new GlideAjax('CatalogAssetHelper');
    ga.addParam('sysparm_name', 'getUserComputers');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXMLAnswer(function(response) {
        if (response) {
            // Set the List Collector value with the CSV string of IDs
            g_form.setValue('your_computer_list_variable', response); 
        }
    });
}


How it works:

  1. When the Manager selects the employee in the form.

  2. The script asks the server: "Give me all computer IDs assigned to this person."

  3. The server returns "sys_id_1, sys_id_2".

  4. The form auto-selects those items in the box.

If this solution helps you automate the form, please mark it as Accepted Solution.

Best regards,
Brandão.

View solution in original post

2 REPLIES 2

Itallo Brandão
Tera Guru

Hi @BrianS344076034 ,

Yes, absolutely! This is a great User Experience improvement. You can automatically select the items in the List Collector so the manager just has to "Review" them instead of searching for them.

Since the List Collector expects a comma-separated string of Sys_IDs to set the values, you need a Client Script (to handle the form) and a Script Include (to query the database).

Here is the recipe:

Step 1: Client Callable Script Include

Create a new Script Include.

  • Name: CatalogAssetHelper

  • Client callable: Checked (True)

 
var CatalogAssetHelper = Class.create();
CatalogAssetHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserComputers: function() {
        var userId = this.getParameter('sysparm_user_id');
        var computerIds = [];

        var gr = new GlideRecord('cmdb_ci_computer');
        gr.addQuery('assigned_to', userId);
        // Optional: Add status query (e.g., only 'In Use')
        // gr.addQuery('install_status', 1); 
        gr.query();

        while (gr.next()) {
            computerIds.push(gr.getUniqueValue());
        }

        // Return a comma-separated string of SysIDs
        return computerIds.join(',');
    },

    type: 'CatalogAssetHelper'
});

Step 2: Catalog Client Script

Create a script on your Catalog Item.

  • Type: onChange

  • Variable name: The variable where you select the User (e.g., leaver or requested_for).

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

    // Call the server to get assets for this user
    var ga = new GlideAjax('CatalogAssetHelper');
    ga.addParam('sysparm_name', 'getUserComputers');
    ga.addParam('sysparm_user_id', newValue);
    ga.getXMLAnswer(function(response) {
        if (response) {
            // Set the List Collector value with the CSV string of IDs
            g_form.setValue('your_computer_list_variable', response); 
        }
    });
}


How it works:

  1. When the Manager selects the employee in the form.

  2. The script asks the server: "Give me all computer IDs assigned to this person."

  3. The server returns "sys_id_1, sys_id_2".

  4. The form auto-selects those items in the box.

If this solution helps you automate the form, please mark it as Accepted Solution.

Best regards,
Brandão.

This worked out perfectly
i have to do the same for mobile phones. which we store under peripherals, my assumption i can do the same scripts, but the first one i point to the peripheral table 

THANK YOU!!