How to populate the asset ID
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 12:13 AM
Hi All,
I am working on one of the form in this form I have Asset Name field it is referring to cmdb_ci_service table, and I have one more field called "Asset Id" in this field I need to populate the Selected asset name's id(Asset Tag).
I tried with the onChange od Asset Name like below

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 12:19 AM
@vinuth v Could you please try updating the scripts, as follows.
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var assetname=g_form.getValue('asset_name');
var g = new GlideAjax('getAssetID');
g.addParam('sysparm_name', 'assetID');
g.addParam('sysparm_asset_name', assetname);
g.getXML(Displayresult);
function Displayresult(response) {
var result = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('asset_id', result);
}
}
Script include:
var getAssetID = Class.create();
getAssetID.prototype = Object.extendsObject(AbstractAjaxProcessor, {
assetID : function(){
var result;
var aName = this.getParameter('sysparm_asset_name');
var gr=new GlideRecord('cmdb_ci_service');
gr.addQuery('sys_id',aName);
gr.query();
if(gr.next())
{
result = gr.getValue('asset_tag');
}
return result;
},
type: 'getAssetID'
});
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 02:10 AM
Hi @vinuth v
Let's change this line
From
var result = response.responseXML.getElementsByTagName("result");
To
var result = response.responseXML.documentElement.getAttribute("answer");
And give the Auto-populate feature a try as well. You can autofill the Asset Tag without a single line of code.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-15-2023 02:14 AM
Hi @vinuth v
Did you check OOTB asset creation when creating a CI.
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]
****************************************************************************************************************