Discovery is not overwriting some values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello,
Working with discovery pattern for a custom table we are trying to update or create new hardware models in the system, from the patter we get the model name and in a pre script we search in the model table to see if it exist and if it does not exist create it, the problem is that if the field is already populated the model field in the CI won't change.
In the example below the first CI did have the model_id field populated before the discovery, that model is not the correct and must be overwrite by the pattern, in the second CI the model was empty and the pattern worked as expected:
the code below get's the company sys id in the line 7, then in the line 16 start checking for the model, if it finds it, it just set the model sys id in the model_id variable in the json, if not it create it in the line 25
I need to know what is missing in the process to overwrite the model in the CIs that already have one
Thanks and regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @Mid Developer ,
Can you try with below code and let me know how it goes?
var populateModelDetails = function() {
var nasuniNameID = gs.getProperty("(MCY)nasuni.company.name");
var nameGR = new GlideRecord("core_company");
nameGR.addQuery("sys_id", nasuniNameID);
nameGR.query();
nameGR.next();
var nasuniName = nameGR.name;
if (payloadObj.hasOwnProperty('items')) {
var model, manufacturer;
for (var i = 0; i < payloadObj.items.length; i++) {
if (payloadObj.items[i].className.includes('u_cmdb_ci_nasuni')) {
model = payloadObj.items[i].values.model_id;
manufacturer = payloadObj.items[i].values.manufacturer_nasuni;
var gr = new GlideRecord("cmdb_hardware_product_model");
gr.addQuery("display_name", nasuniName + " " + model);
gr.query();
if (gr.next()) {
// Model exists - update payload
payloadObj.items[i].values.model_id = gr.sys_id + "";
// ADDED: Update the actual CI record
var ciRecord = new GlideRecord("cmdb_ci_hardware");
if (ciRecord.get(payloadObj.items[i].sys_id)) {
ciRecord.model_id = gr.sys_id;
ciRecord.update();
}
} else {
// Model doesn't exist - create it
var newHardModel = new GlideRecord("cmdb_hardware_product_model");
newHardModel.initialize();
newHardModel.name = model;
newHardModel.manufacturer = nasuniNameID;
newHardModel.insert();
payloadObj.items[i].values.model_id = newHardModel.sys_id + "";
// ADDED: Update the actual CI record
var ciRecord = new GlideRecord("cmdb_ci_hardware");
if (ciRecord.get(payloadObj.items[i].sys_id)) {
ciRecord.model_id = newHardModel.sys_id;
ciRecord.update();
}
}
}
}
}
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 weeks ago
Thank you for the try, but it does not work, the payload itselft does not have the Ci's sys id, in the code we may look it using the name or other identifier but if the record is new that will not work, I'm trying to find the reason why the process is not overwriting tha value ignoring the new one
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
a week ago - last edited a week ago
@Mid Developer - Thanks for the response. One question. You have mentioned that this is a pre-sensor script. Should we just skip the code for update & create of model on the "cmdb_hardware_product_model" table. Ideally the IRE will take care of the creations/updations.
Pre-Sensor scripts in general are used to modify the payload before its passed to IRE to make decision of creation/updates.
I would try this approach. Eliminate this script step as the model details are anyways coming in the payload and create a new identification rule on the product model table to ensure the updates and creations are done properly.
Thanks,
Pavan BV
