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

Hello,

I would strongly suggest you use the above business rule code.and create a new one with modifications in it as per your requirement.

This business rule is written on the ASSET table for creating and updating CI record.

This may help you a lot.

Please keep posted if more help required.

Regards,

Yash Agrawal

Hi Yash,

Unfortunately do to an enhancement we made months ago it broke creation of CI's of new assets without us knowing until recently. Now we're trying to do a one time clean-up for those assets that should have  CI.

We could update the business rule to create the CI on update, however that means manually going though all the assets and clicking update. Now we're at the point of just creating a script to handle it.

Thank you for your input!

Dom

Hello MFG,

So, if you want help, please provide the details description of the issue.

So that I can also help you, to achieve it.

Regards,

Yash Agrawal

Hi Yash,

In short we're looking to create CI's and associate them to asset records that do not have CI's for specific model categories. The details on this query are above in my code snippet.

Thank you!

Dom

Hello ,

did you check how much parameter CreateCi() function wants.

second point why you gave ^ORmodel_category=Null

var queryStr = 'ciISEMPTY^model_category!=fed01818db93ff00c0ede66505961962^ORmodel_category=NULL

It should be 

^model_category==NULL

 

One more error if you want to pass asset sys_id in createCi() function then pass it like

CreateCi(assets_no_ci.sys_id)

So that it will pass sys_id of asset record to function.

 

please try to check the mentioned point.

Your query will. get reaolved.

Regards

Yash Agrawal