How to populate the asset ID

vinuth v
Tera Expert

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).

vinuthv_0-1700035745385.png

I tried with the onChange od Asset Name like below

 

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.getElementsByTagName("result");
        g_form.setValue('asset_id', result);
 
    }
 }
 
And 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.asset_tag;
}
return result;
},
 
    type: 'getAssetID'
});
 
I tried with the above script and it is not working as expected, 
 
Please any one suggest me.
Thanks in advance,
Vinuth

 

3 REPLIES 3

Sandeep Rajput
Tera Patron
Tera Patron

@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.

Tai Vu
Kilo Patron
Kilo Patron

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.

TaiVu_0-1700042965977.png

 

Cheers,

Tai Vu

Dr Atul G- LNG
Tera Patron
Tera Patron

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]

****************************************************************************************************************