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

Sai Kumar B
Mega Sage

@Community Alums 
1.) Create a Client callable script include with the below code and pass the variable names accordingly

var getAssetProperties= Class.create();
getAssetProperties.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getAssetDetails: function() {
                var arr = [];
		var grAsset = new GlideRecord('cmdb_ci_computer');
		if(grAsset.get('assigned_to', this.getParameter('user'))) {
			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'
	
});

2.) Write an onChange() client script with the below code on an Affected user variable

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if(isLoading || newValue=='') {
		return;
	}
if(newValue) {
var callScriptInclude = new GlideAjax('getAssetProperties');
callScriptInclude.addParam('sysparm_name', 'getAssetDetails');
callScriptInclude.addParam('user', newValue);
callScriptInclude.getXMLAnswer(_handleResponse);

function _handleResponse(response) {
		g_form.setValue('<pass mrvs name>', response);
	}
}

 

Hi @Sai Kumar B 

Thanks for the reply. Will try this code and get back to you.

@Sagar Garige 

Modify your script include as below

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'
	
});

Thank you soo much. It is working as expected.