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

Prashant Ahire
Kilo Sage

Hello @Khill 

You cannot return Array from script include to client script

Instead you can convert this to string and pass to client script and then split in array.

 

Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

 

Hello @Khill   you can use JSON.stringify(deviceList) to convert the array to a string and JSON.parse on the client side to get back the value back

 

Hope this helps

--

Bala Guthy

 

Khill
Tera Contributor

I added the JSON.stringify in the script include. I'm receiving  a console error.  "TypeError: Cannot read properties of undefined. 

Anil Lande
Kilo Patron

Hi,

Please share which Ui you are using? Catalog form, Classic Form?

Is your name field have default value?

Have you tried adding alert/logs and logs to check what values you are passing and what your script sending from script include?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande