Auto Populate List Collector with devices assigned to user using Script Include and Catalog Client
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 11:28 AM
Catalog Client - onLoad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 04:58 AM
This is for a Catalog item.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 10:46 PM - edited 01-16-2024 10:50 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2024 05:05 AM
I made the changes you suggested and the list collector is not populating. Receiving a TypeError : Cannot read properties of undefined.
