- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 01:08 PM
Hello!
I'm trying to create a fix script that will create CI records on assets (alm_asset) that do not have a CI.
I'm using this Business Rule and Script Include to aid me in actually creating the CI.
https://<instance-name>.service-now.com/nav_to.do?uri=sys_script.do?sys_id=26ebfeef1b3210002502fbcd2c07136f https://<instance-name>.service-now.com/nav_to.do?uri=sys_script_include.do?sys_id=19c0f3603703100044e0bfc8bcbe5d9b
Here is my script below. I'm basically searching for all of my assets then going to loop though each calling the script include to create the CI for each.
var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();
while (assets_no_ci.next()) {
(new AssetandCI()).createCI(assets_no_ci);
gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}
However when I run the script it gives me an error:
*** Script: Asset count: 1
Evaluator: org.mozilla.javascript.EvaluatorException: GlideRecord.insert() - invalid table name: null (sys_script_include.19c0f3603703100044e0bfc8bcbe5d9b.script; line 180)
EvaluatorException(JavaScript evaluation error on:
var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single template
assets_no_ci.orderBy('model_category');
assets_no_ci.query();
while (assets_no_ci.next()) {
(new AssetandCI()).createCI(assets_no_ci);
gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}
)
[0:00:00.055] Total Time
Any thoughts or help would be appreciated.
Thanks!
Dom
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 09:53 AM
so that's the issue, it doesn't know on what cmdb_ci table to create the CI's. This value should be stored in the model category and is what the following does. it dot walks to the value
ciClass = asset.model_category.cmdb_ci_class.toString();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-23-2020 01:39 PM
Hi Dom,
Have you / someone in your organisation edited AssetandCI script include as line 180 (which is erroring) is a curly bracket in my OOB script.
My only other suggestion, do the alm_asset records have a model category and do those categories have a CI class?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 04:11 AM
Hey Kieran,
Thanks for the response!
We have not edited the script include AssetandCI, it is as is from OOB.
The alm_asset records in my query do indeed have a model category and do have a CI class.
Thanks,
Dom

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 06:36 AM
Interesting, could you try the below script? It's an extract of the inner workings of the AssetandCI script include, just removed some of the boilerplate code and stripped it down to its core.
If it errors, likely the issue is with one of the further referenced SIs (AssetCMDBUtil or AssetAndSynchronizer)
var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL^model_category!=996b568ec3102000b959fd251eba8f19^model_category!=218323293743100044e0bfc8bcbe5d61^model_category!=35bf2d4137101000deeabfc8bcbe5dbd^model_category!=c9d5bc50c3031000b959fd251eba8fbc^model_category!=83c92540732623008b516cb63cf6a7b1^model_category!=58fdb454c3031000b959fd251eba8ff0';
var assets_no_ci = new GlideRecord('alm_asset');
//assets_no_ci.addEncodedQuery(queryStr); //Commented to test with a single record
assets_no_ci.addQuery('serial_number', '1234567890'); //test this script with a single record
assets_no_ci.orderBy('model_category');
assets_no_ci.query();
while (assets_no_ci.next()) {
createAsset(assets_no_ci);
gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
}
function createAsset(asset){
if (ciClass != '') {
var ciSysId;
var ci = new GlideRecord(ciClass);
ci.initialize();
ci.asset = asset.sys_id;
ci.name = asset.model.name;
// Populate manufacturer
ci.manufacturer = asset.model.manufacturer;
var sync = new AssetAndCISynchronizer();
sync.syncRecordsWithoutUpdate(asset, ci, 'cmdb_ci', false);
var assetCMDBUtil = new AssetCMDBUtil();
if (asset.install_status != 2
&& !gs.nil(asset.serial_number)
&& assetCMDBUtil.isSerialNumberMandatory(asset.model_category)) {
ci.name = asset.serial_number + ' - ' + asset.model.name;
ciSysId = assetCMDBUtil.createCIUsingIRE(ci);
} else {
ciSysId = ci.insert();
}
asset.ci = ciSysId;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-24-2020 06:53 AM
When I ran the snippet, I got the below. I double checked the assets in my query and all have a class of hardware.
Evaluator: org.mozilla.javascript.EcmaError: "ciClass" is not defined.
Caused by error in script at line 33
30:
31: function createAsset(asset){
32:
==> 33: if (ciClass != '') {
34: var ciSysId;
35: var ci = new GlideRecord(ciClass);
36: ci.initialize();
Evaluator: org.mozilla.javascript.EcmaError: "ciClass" is not defined.
Caused by error in script at line 26
23: assets_no_ci.query();
24:
25: while (assets_no_ci.next()) {
==> 26: createAsset(assets_no_ci);
27: gs.log("Adding CI to Asset: " + assets_no_ci.display_name);
28: }
29:
[0:00:00.061] Total Time
Thanks,
Dom