Creating models on data import

Yadin
Tera Contributor

Working with data import for computer assets.  In the Transform map, I have an onBefore script to handle making the Model for the Computer if one does not already exist.  Not all the script included here, as not relevant to parsing make/model/number out of the input data, and all that is tested working fine.  The issue is, SN isn't actually making the Model entry, but it's not erroring to explain why.  Running as a test in Background Scripts, it pretends everything worked as I see this output:

*** Script: Scheduling: Generate Assets: MacPro (MacPro4,1) for: 2022-11-22 19:53:26

my gs.info tells me: should be Apple Inc. MacPro (MacPro4,1)

So one issue is the display_name is not getting made properly.  The other issue is, it's not making the entry in the model table at all.  I'm not sure why it says "generate assets" though either.  Here is the portion of the script that should be making the entry:

var grNew = new GlideRecord("cmdb_model");

grNew.initialize();

grNew.name = model;

grNew.sys_created_by = "AllSight";

grNew.manufacturer = manu;

grNew.cmdb_model_category = "Computer";

grNew.insert();

gs.info("should be "+manu+" "+model);

Seems pretty straight forward, and I've used similar method to create User records with no issue.  So why won't it actually make the model record?

I'm trying to do it this way so the model is made "properly" because if you just let import create models from what is put in the model field, they lack manufacturer and then proper display name (calculated manufacturer and model name together).  That in turn creates other asset management issues as the asset record isn't made properly for the computer record.  Wish this wasn't so darned complicated...

So, any thoughts or advice why this isn't working and how to make it create these records would be greatly appreciated. 

10 REPLIES 10

MercBuilding
Giga Guru

Hi,

  I tried your script with some modifications and it created the model in Product model table. Include name and manufacturer in single quotes as 'model' and 'manu' and it will create the model. Also if you are creating the models for HAM Pro try creating them directly in Hardware model(cmdb_hardware_product_model) table instead of product model and make sure Manufacturer record is already present in the system otherwise you need to create the manufacturer record first and then create the model record with same manufacturer.

Mark my answer as correct if it helps you.

I'm not sure how you tested, but this does not work.  I found some of the reason why I was not seeing the models as expected, but then I was unable to determine what the remaining problems were.  The above does NOT work as for some reason creating the model (cmbd_model) by script refuses to write the manufacturer field.  No idea why, it just doesn't do it.  Also, the Display Name field is supposed to be (based on documentation and behavior traditionally) automatically made from the Manufacturer+Name.  This does not happen, and again it's unclear why it's left blank.  Oddly this field is NOT a calculated field like with users, so I have no idea how it could ever be populated, but it's also apparently the most important field, so this seems to be a design issue.  These problems led me to think the models were not being made, because they were missing most of their fields and therefore I could not see them.  Lastly, it has become clear that there is no duplicate checking in the models as expected.  It will simply make infinite records that are identical in all ways.  This is very perplexing as it seems to throw data management out the window.  I have in fact found many threads now that confirm these problems.

So, I have started working around these deficiencies in other ways.  Models are now being made by transform map on import.  This resolves the manufacturer not being written.  Script has been added to create and populate the Display Name since it's not being properly populated by the platform.  Script has been added onBefore to the transform to look for an existing record and skip row if it already exists. 

I would not have expected to have to code my way around something this seemingly basic to the platform.  I'm not sure why things as simple as asset import are so obtuse and complicated with results that are lacking in the expected advertised features.  It's clear now why customers ask for us to create an import plugin, because it's extremely difficult to just pull in asset records.

MercBuilding
Giga Guru

Hi,

In above reply i stated that you need to keep a check on Manufacturer, if its not present then you have to create the manufacturer record first and then create the model. Also the problem you highlighted about the duplicate hardware models, yes it is true that Servicenow didnt write any script that will stop creating new models if they are already not present so generally in these cases we write a business rule to avoid duplicate creation of models.

The manufacturers exist in the linked company table, that is not relevant to the case.  As stated you simply can't write that field by script for unknown reason, but a transform map can write to it.  I verified this by Background Script.  If you find otherwise, I don't know why since the platform is not explaining the failure.