- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 05:22 AM - edited 10-04-2023 12:14 PM
Hi Team,
I have requirement for service catalog. I would need to populate list of assigned assets to the user.
It should be a onchange client script and if user has any assets assigned should populate and show up the name of assets.
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 06:30 AM
Hello @servicenow_devo
I have achieved this by using variable type as a 'HTML'.
Can you check if this works for you....!!
Step 1 : Created two variables
A] Name : User (user)
Type : Reference
Table : sys_user
B] Name : My Assets (my_assets)
Type : HTML
Read only : true
Portal view :
Step 2 : Write Client script and script include as below
Script include :
Name - assetDetailUtils
var assetDetailUtils = Class.create();
assetDetailUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssetDetails: function() {
/*1. Get user from client script */
var user = this.getParameter('sysparm_user');
/*2. Declare one array to send results */
var assetarray = [];
/*3. Glide record on Hardware table - you can use your table */
var grHardware = new GlideRecord('alm_hardware');
grHardware.addQuery('assigned_to', user); //query with assigned to is user
grHardware.query();
while (grHardware.next()) {
/* 4. Get detalis in JSON */
var assetDetails = {};
assetDetails.asset_tag = grHardware.getValue('asset_tag').toString();
assetDetails.serial_num = grHardware.getValue('serial_number').toString();
/* 5. Push the JSON in Array */
assetarray.push(JSON.stringify(assetDetails));
}
/* Return Array */
return assetarray.join('|');
},
type: 'assetDetailUtils'
});
OnChange client script on 'User' variable :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getValue('user');
var values;
var ga = new GlideAjax('assetDetailUtils'); //script include
ga.addParam('sysparm_name', 'getAssetDetails'); //script include function
ga.addParam('sysparm_user', user);
ga.getXMLAnswer(callBack);
function callBack(answer) {
var result = answer.split('|');
for (var i = 0; i < result.length; i++) {
if (values) {
values = values + '<p>' + 'Asset Tag : ' + JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+ JSON.parse(result[i]).serial_num +'</p>';
} else {
values = '<p>' + 'Asset Tag : ' + JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+ JSON.parse(result[i]).serial_num +'</p>';
}
}
g_form.setValue('my_assets', values);
}
}
Output :
Hope this works...!!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 04:10 AM - edited 10-03-2023 04:10 AM
Hi @Manoj89
It's still not giving me required output. If you can help me with script Include and code it will be helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 08:54 AM
Rich text label is just a label to provide some message or information.
You cant change it dynamically.
If you want to display asset assigned to user then use Multi Row variable set.
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2023 06:30 AM
Hello @servicenow_devo
I have achieved this by using variable type as a 'HTML'.
Can you check if this works for you....!!
Step 1 : Created two variables
A] Name : User (user)
Type : Reference
Table : sys_user
B] Name : My Assets (my_assets)
Type : HTML
Read only : true
Portal view :
Step 2 : Write Client script and script include as below
Script include :
Name - assetDetailUtils
var assetDetailUtils = Class.create();
assetDetailUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getAssetDetails: function() {
/*1. Get user from client script */
var user = this.getParameter('sysparm_user');
/*2. Declare one array to send results */
var assetarray = [];
/*3. Glide record on Hardware table - you can use your table */
var grHardware = new GlideRecord('alm_hardware');
grHardware.addQuery('assigned_to', user); //query with assigned to is user
grHardware.query();
while (grHardware.next()) {
/* 4. Get detalis in JSON */
var assetDetails = {};
assetDetails.asset_tag = grHardware.getValue('asset_tag').toString();
assetDetails.serial_num = grHardware.getValue('serial_number').toString();
/* 5. Push the JSON in Array */
assetarray.push(JSON.stringify(assetDetails));
}
/* Return Array */
return assetarray.join('|');
},
type: 'assetDetailUtils'
});
OnChange client script on 'User' variable :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var user = g_form.getValue('user');
var values;
var ga = new GlideAjax('assetDetailUtils'); //script include
ga.addParam('sysparm_name', 'getAssetDetails'); //script include function
ga.addParam('sysparm_user', user);
ga.getXMLAnswer(callBack);
function callBack(answer) {
var result = answer.split('|');
for (var i = 0; i < result.length; i++) {
if (values) {
values = values + '<p>' + 'Asset Tag : ' + JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+ JSON.parse(result[i]).serial_num +'</p>';
} else {
values = '<p>' + 'Asset Tag : ' + JSON.parse(result[i]).asset_tag + ' | ' +'Serial Number : '+ JSON.parse(result[i]).serial_num +'</p>';
}
}
g_form.setValue('my_assets', values);
}
}
Output :
Hope this works...!!!
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
