- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-17-2024 07:47 AM
Hi there,
I am getting an undefined error when trying to look at a JSON payload.
The payload is built in the same script, and to debug I am trying to call the name of the payload to check that the name of the CI can be used.
This is my test script:
var ax = new GlideRecord('x_ccluk_axonious_axonious_api');
ax.addEncodedQuery();
ax.setLimit(1);
ax.query();
gs.info("MigrateUTIL [6] run");
while (ax.next()) {
try {
var lastDiscovered = new GlideDateTime(ax.u_most_recent_discovery);
var os = ax.u_os_edition.toString().trim() != "" ? ax.u_operating_system.toString() + ' ' + ax.u_os_edition.toString() : ax.u_operating_system.toString();
var fqdn = ax.u_fqdn_host_name;
var className = ax.u_class.toString();
var payload = {
"items": [{
"className": className,
"values": {
'name': ax.u_name.toString(),
'serial_number': ax.u_serial_number.toString(),
'manufacturer': ax.u_manufacturer.toString(),
//'fqdn': this.isValidFQDN(fqdn) ? fqdn : '',
'model_id': ax.u_model_id.toString(),
'os': os,
'os_version': ax.u_os_version.toString(),
'ip_address': ax.u_ip_address.toString(),
'mac_address': ax.u_mac_address.toString(),
'last_discovered': lastDiscovered.toString(),
'install_date': ax.u_first_seen.toString(),
'install_status': '1',
'company': ax.u_company.toString()
}
}]
};
gs.info("MigrateUTIL [34] run");
var parser = new global.JSONParser();
var parsed = parser.parse(payload);
var ciName = parsed.items[0].values[0].name;
gs.info(ciName);
// var ci_sys_id;
// gs.info("MigrateUTIL [37] run");
// var inputPayload = new global.JSON().encode(payload); //JSON formatted string
// gs.info("MigrateUTIL [39] run");
// gs.info(payload.items.values.ip_address);
} catch (error) {
gs.error("Axonius migrate error: " + error.message);
}
}Which outputs
x_ccluk_axonious: MigrateUTIL [6] run
x_ccluk_axonious: MigrateUTIL [34] run
x_ccluk_axonious: Axonius migrate error: Cannot read property "0" from undefined
I have been testing with this code for a few days and have not had any luck yet, so help would be really appreciated.
This is a scoped application, and the idea would be to then use the IRE API to feed the data into the CMDB, however the payload gets loaded with data, but cannot be called into the IRE Scripts in the live code, below:
var AxoniusAPIMigrateUtil = Class.create();
AxoniusAPIMigrateUtil.prototype = {
initialize: function() {
},
isValidFQDN: function(fqdn) {
var fqdnPattern = /^(?=.{1,253}$)(([A-Za-z0-9]+(-[A-Za-z0-9]+)*\.)+[A-Za-z]{2,63})$/;
return fqdnPattern.test(fqdn);
},
migrateByBrandLatestImportSet: function(brand) {
gs.info("MigrateUTIL [14] run");
var importSet = '';
var axoimp = new GlideRecord('x_ccluk_axonious_axonious_api');
axoimp.addEncodedQuery();
axoimp.orderByDesc('u_import_set_id');
axoimp.setLimit(1);
axoimp.query();
while (axoimp.next()) {
importSet = axoimp.u_import_set_id.getDisplayValue();
}
var ax = new GlideRecord('x_ccluk_axonious_axonious_api');
ax.addQuery('u_import_set_id.number', importSet);
ax.addEncodedQuery("u_classISNOTEMPTY^u_class!=N/A^u_class!=NULL");
ax.query();
gs.info("MigrateUTIL [28] run");
while (ax.next()) {
try {
var lastDiscovered = new GlideDateTime(ax.u_most_recent_discovery);
var os = ax.u_os_edition.toString().trim() != "" ? ax.u_operating_system.toString() + ' ' + ax.u_os_edition.toString() : ax.u_operating_system.toString();
var fqdn = ax.u_fqdn_host_name;
var className = ax.u_class.toString();
var payload = {
"items": [{
"className": className,
"values": {
'name': ax.u_name.toString(),
'serial_number': ax.u_serial_number.toString(),
'manufacturer': ax.u_manufacturer.toString(),
'fqdn': this.isValidFQDN(fqdn) ? fqdn : '',
'model_id': ax.u_model_id.toString(),
'os': os,
'os_version': ax.u_os_version.toString(),
'ip_address': ax.u_ip_address.toString(),
'mac_address': ax.u_mac_address.toString(),
'last_discovered': lastDiscovered.toString(),
'install_date': ax.u_first_seen.toString(),
'install_status': '1',
'company': ax.u_company.toString()
}
}]
};
gs.info("MigrateUTIL [55] run");
// if (className == "cmdb_ci_esx_server") {
// payload.items[0].values.correlation_id = 'ax.u_correlation_id';
// }
var ci_sys_id;
gs.info("MigrateUTIL [62] run");
var inputPayload = new global.JSON().encode(payload);//JSON formatted string
gs.info("MigrateUTIL [64] run");
gs.info(payload);
var identifyJson = sn_cmdb.IdentificationEngineScriptableApi.identifyCI(inputPayload);
var identify = global.JSON.parse(identifyJson);
var operation = identify.items[0].operation;
gs.info("MigrateUTIL [68] run");
if (operation == 'UPDATE') {
gs.info("MigrateUTIL [78] run");
var cmdbgr = new GlideRecord('cmdb_ci');
cmdbgr.get(identify.items[0].sysId);
if (lastDiscovered > cmdbgr.last_discovered) {
sn_cmdb.IdentificationEngineScriptableApi.createOrUpdateCI('Axonius', inputPayload);
ci_sys_id = identify.items[0].sysId;
gs.info("Axnoius " + operation + ": " + identifyJson);
gs.info("MigrateUTIL [78] run");
}
} else {
gs.info("MigrateUTIL [82] run");
var outputApi = sn_cmdb.IdentificationEngineScriptableApi.createOrUpdateCI('Axonius', inputPayload);
var output = global.JSON.parse(outputApi);
ci_sys_id = output.items[0].sysId;
gs.info("Axnoius migrate " + output.items[0].operation + ": " + outputApi);
}
// var ci_sys_id = output.items[0].sysId;
if (ci_sys_id && ci_sys_id.length > 0 && ci_sys_id.toLowerCase() != 'unknown') {
gs.info("MigrateUTIL [91] run");
gs.info("Axonius script created / updated: " + ci_sys_id);
ax.u_related_ci = ci_sys_id;
ax.update();
// Update Created By/Updated By to user svc_Axonius
// var cmdbGlideRecord = new GlideRecord('cmdb_ci');
// if(cmdbGlideRecord.get(ci_sys_id)) {
// cmdbGlideRecord.autoSysFields(false);
// if (operation == 'UPDATE') {
// cmdbGlideRecord.sys_updated_by = 'svc_Axonius';
// } else {
// cmdbGlideRecord.sys_created_by = 'svc_Axonius';
// }
// cmdbGlideRecord.update();
//
}
} catch (error) {
gs.error("Axonius migrate error: " + error.message);
}
}
},
type: 'AxoniusAPIMigrateUtil'
};The script will fail when this line is run and returns with the undefined error:
var inputPayload = new global.JSON().encode(payload);//JSON formatted stringThanks in advance !
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 07:49 AM
Well the in the solution for that thread is stated that:
the property is already string so don't use JSON.stringify()
I used as I was testing in background script
So it was used just to simulate the situation where the data would be loaded from a property, where it would be a string.
As for IRE, I would look into Script Include global.CMDBTransformUtil which contains functions that allow you to utilize a regular transform map (and its fields maps) to build payloads and to consume those payloads. SN uses the functions in that Script Include when they want to import data into the CMDB through Import Set Row tables. E.g. S.C.C.M. integration used to use it.
All in all, once you figure out what the Script Include does, it can make your life easier as it will enable you to make the payloads "configurable".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2024 05:30 AM
You're welcome, glad that it works!
As for the tomfoolery, probably just copied some not-printable/invisible Unicode character with the text 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 07:49 AM
Well the in the solution for that thread is stated that:
the property is already string so don't use JSON.stringify()
I used as I was testing in background script
So it was used just to simulate the situation where the data would be loaded from a property, where it would be a string.
As for IRE, I would look into Script Include global.CMDBTransformUtil which contains functions that allow you to utilize a regular transform map (and its fields maps) to build payloads and to consume those payloads. SN uses the functions in that Script Include when they want to import data into the CMDB through Import Set Row tables. E.g. S.C.C.M. integration used to use it.
All in all, once you figure out what the Script Include does, it can make your life easier as it will enable you to make the payloads "configurable".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-23-2024 08:06 AM
Hi, so I did work it out, where I had been copy and pasting this code around, the IdentificationEngine API wasnt copied across as an API call, but as text? Essentially I realized that the text was not highlighted green in the Util, so I rewrote it, got it highlighting green, and it works as expected now. I'm chalking that up to some quirky ServiceNow tomfoolery.
The number of Rabbit holes I have gone down, for that to be the issue!
Appreciate your help -O-.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-25-2024 05:30 AM
You're welcome, glad that it works!
As for the tomfoolery, probably just copied some not-printable/invisible Unicode character with the text 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2024 01:02 PM - edited ‎10-25-2024 08:19 AM
Great to see you’re getting closer to a solution! If you’re still hitting undefined errors, double-check that all object properties exist before accessing them. Sometimes, missing data can cause these issues.
If you're dealing with properties that have long-term rentals toilets, ensure adequate toilet facilities are available to avoid potential disputes and legal issues.
