- 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
09-22-2023 06:51 AM - edited 09-22-2023 06:52 AM
Hi @servicenow_devo ,
Check if below community post helps you, seems your requirement matches with the same.
Please check and Mark Helpful and Correct if it really helps you.
Regards,
Mayur Shardul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 08:15 AM
set onCHange script for the field requested_for if that is where you are going to change users
function onChange(control, oldValue, newValue, isLoading) {
var assets = [];
var ol;
var gr = new GlideRecord('cmdb_ci_computer');
gr.query('assigned_to', newValue);
gr.query();
while (gr.next()){
assets.push(gr.asset_tag);
}
//add to your form a rich text label variable
if(assets.length >0){
assets.forEach(createOl);
g_form.setDisplay('rich_text', true);
g_form.setLabelOf('rich_text', 'Dear,<br/><br/>You already have PC(s)<br/><ol>'+ol+'</ol> assigned');
}else{
g_form.setDisplay('rich_text', false);
}
}
function createOl(element, index){
ol += '<li id='+index+'>'+element+'</li>';
}Note: this script was written considering maintain item form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 03:48 AM
Hi thanks for reply.
The logic seems nice and but I was not able to get the output. It's not showing any text box for the requirement.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 05:41 AM
Hi,
Poor me, using glide record in client script is not recommended, can you please move the glide record script to script include and call it using glideajax, it's working perfectly for me, you can do debugging and check what you are getting as a result.
