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.

Display all the assets assigned to User

Community Alums
Not applicable

Hi Experts,

We have a requirement, it is to Display all the assets assigned to Affected user in a catalog itemAffected user is a reference variable pointing to (sys_user table).

If user has one asset it should display as below image (1)

Sagar12_0-1666688500291.png

If the affected user has multiple devices it should display as below image (2)

Sagar12_1-1666689557444.png

Note:  Assets should be displayed from cmdb_ci_computer table.

11 REPLIES 11

@James Rostron Please use the following modified script include code

var getAssetProperties= Class.create();
getAssetProperties.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getAssetDetails: function() {
                var arr = [];
		var grAsset = new GlideRecord('cmdb_ci_computer');
                       grAsset.addQuery('assigned_to', this.getParameter('user'));
                       grAsset.query();
                       while(grAsset.next()) {
			arr.push({'<pass asset tag variable name here>' : grAsset.getValue('asset_tag'),  '<pass manufacturer variable name here>' : grAsset.getValue('manufacturer'),    '<pass model id variable name here>' : grAsset.getValue('model_id'), '<pass serial number variable name here>' : grAsset.getValue('serial_number')}) //Store the details in JSON format
		}
return JSON.stringify(arr);
	},
    type: 'getUserPropertiesAjax'
	
});

James Rostron
Tera Contributor

For anyone else drawn to this article I managed to fix the code so that multiple assets are returned.

See updated script include below:

NB: Update with your variable names

 

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

    getAssetDetails: function() {
        var arr = [];
        var grAsset = new GlideRecord('cmdb_ci_computer');
		grAsset.addQuery('assigned_to', this.getParameter('user'));
        grAsset.query();
        while (grAsset.next()) {
            arr.push({
                'name': grAsset.getValue('name'),
                'manufacturer': grAsset.getDisplayValue('manufacturer'),
                'model': grAsset.getDisplayValue('model_id'),
                'serial_number': grAsset.getValue('serial_number')
            }) //Store the details in JSON format
        }
        return JSON.stringify(arr);
    },
    type: 'getAssetProperties'
});