Display all the assets assigned to User
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 02:26 AM
Hi Experts,
We have a requirement, it is to Display all the assets assigned to Affected user in a catalog item. Affected user is a reference variable pointing to (sys_user table).
If user has one asset it should display as below image (1)
If the affected user has multiple devices it should display as below image (2)
Note: Assets should be displayed from cmdb_ci_computer table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 03:44 AM
@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);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 04:51 AM
Hi @Sai Kumar B
Thanks for the reply. Will try this code and get back to you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2022 07:54 PM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-26-2022 02:37 AM
Thank you soo much. It is working as expected.