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.

Auto Populate List Collector with devices assigned to user using Script Include and Catalog Client

Khill
Tera Contributor

Catalog Client - onLoad

 

function onLoad() {

    var userID = g_form.getValue('name');

        var ga = GlideAjax('GetAssignedDevices');
        ga.addParm('sysparm_name', 'getUserDevices');
        ga.addParm('sysparm_userID', userID);
        ga.getXMLAnswer(getResponse);

    function getResponse(response){
        var devices = JSON.parse(response);
        g_form.clearOptions('current_devices');
        g_form.addOption('current_devices', devices);
    }
}
 
Script include 
var GetAssignedDevices = Class.create();
GetAssignedDevices.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getUserDevices: function() {
        var deviceList = [];

        var userID = this.getParameter('userID');
        var hardwareGr = new GlideRecord('alm_hardware');
        hardwareGr.addQuery('assigned_to', userID);
        hardwareGr.query();

        while (hardwareGr.next()) {
            deviceList.push(hardwareGr.sys_id.toString());
        }
        return deviceList;

    },
    type: 'GetAssignedDevices'
});
 
Not sure why its not working 
7 REPLIES 7

Khill
Tera Contributor

This is for a Catalog item. 

Iraj Shaikh
Mega Sage

Hi @Khill 

There are a few potential issues with the code you've provided. 

Catalog Client Script - onLoad

In the Catalog Client Script, you are trying to get the user ID and then use a GlideAjax call to fetch the devices. However, there are a few issues:

1. The `addParm` method is incorrect. It should be `addParam`.
2. The `addOption` method is used incorrectly. It should be used inside a loop to add each device as an option to the list collector.
3. The `userID` parameter name in `addParam` should match the one expected in the Script Include (`sysparm_userID`).

Here's the corrected Catalog Client Script:

 

function onLoad() {
    var userID = g_form.getValue('name');

    var ga = new GlideAjax('GetAssignedDevices');
    ga.addParam('sysparm_name', 'getUserDevices');
    ga.addParam('sysparm_userID', userID);
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        var devices = JSON.parse(response);
        g_form.clearOptions('current_devices');
        devices.forEach(function(device) {
            g_form.addOption('current_devices', device);
        });
    }
}

 


Script Include

In the Script Include, there are a couple of issues:

1. The `getParameter` method is used incorrectly. It should include the `sysparm_` prefix when retrieving the parameter.
2. The `return` statement should return a JSON string, not an array, because the client script expects a JSON string that it can parse.

Here's the corrected Script Include:

 

 

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

    getUserDevices: function() {
        var deviceList = [];

        var userID = this.getParameter('sysparm_userID'); // Corrected parameter name
        var hardwareGr = new GlideRecord('alm_hardware');
        hardwareGr.addQuery('assigned_to', userID);
        hardwareGr.query();

        while (hardwareGr.next()) {
            deviceList.push(hardwareGr.sys_id.toString());
        }
        return JSON.stringify(deviceList); // Return a JSON string
    },

    type: 'GetAssignedDevices'
});

 

 

Make sure that the field names used in the script ('name' for the user field and 'current_devices' for the list collector) match the actual field names in your ServiceNow instance. If they don't, you'll need to replace them with the correct field names.

After making these corrections, the script should work as intended, populating the list collector with devices assigned to the user.

Please mark this response as correct or helpful if it assisted you with your question.

I made the changes you suggested and the list collector is not populating.  Receiving a TypeError : Cannot read properties of undefined.