Fix Script to Create CI's on Assets

NFGDom
Mega Sage

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

1 ACCEPTED SOLUTION

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();

find_real_file.png

 

View solution in original post

21 REPLIES 21

I did not check the CreateCI() function yet.

I had copied my query from from the table by clicking on the filter, I can change that to null.

Adding the sys_id seems to have elevated some errors I was having but the CI is still not creating. I'll keep on poking at it.

Thanks!

Dom

Hello NFG,

Just now i Checked in you encoded query,all sys_id of model_Categories are avaibale but 

ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962

 

 above highlighted sys_id is not available.

Will be please remove it from the Encoded query,and try once,

Please keep posted and check again that sys_id is available or not.

Regards,

Yash Agrawal