OnBefore Script import_set undefined
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 02:02 PM - edited 11-30-2023 02:10 PM
Hey Everyone, I am working on a VI integration and am having issues with setting up the OnBefore execution script in the transformation mapping. Here is a copy of the script and the error. Any help would be appreciated!
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
// Handle re-opened VIs
if (action == "update" && target.state == 3) {
// We are re-opening
var now = new GlideDateTime();
target.state = '1';
target.substate = '';
target.reopened = true;
target.last_opened = now.getDisplayValue();
}
//populate ci
var integrationRun = import_set.integrationProcessGr.integration_run;
var sourceInstance = integrationRun.implementation;
var integration = integrationRun.implementation.integration;
var hostAttributeTable = integration.host_attributes_table;
target.integration = integration;
target.integration_instance = sourceInstance;
target.integration_run = integrationRun;
var host = {};
for (var key in source)
if (key.startsWith("u_"))
host[key.substring(2).toUpperCase()] = source.getValue(key);
host["MAC_ADDRESS"] = source.getValue("u_mac_address");
host["HOST_NAME"] = source.getValue("u_host_name");
host["ID"] = source.getValue("u_dev_id");
var hostInfoGR = new sn_vul.HostUtil().initializeHostAttributeRecord(host, hostAttributeTable);
var matchResult = new sn_vul.ImportHost().hostImport(
integrationRun.implementation, host, "ID", integrationRun,
false, false, source.getValue('u_last_assessed'), null, null, hostInfoGR);
if (matchResult.insert)
source.ci_operation = "inserted";
else
source.ci_operation = "updated";
target.cmdb_ci = matchResult.sys_id;
target.src_ci = matchResult.disc_item_id;
})(source, map, log, target);
It errors out on this line var integrationRun = import_set.integrationProcessGr.integration_run;
Here is the error:
com.glide.script.RhinoEcmaError: Cannot read property "integration_run" from undefined
sys_transform_script.f717d64287fe39501aad4047cebb35e8.script : Line(14) column(0)
11: }
12:
13: //populate ci
==> 14: var integrationRun = import_set.integrationProcessGr.integration_run;
15: var sourceInstance = integrationRun.implementation;
16: var integration = integrationRun.implementation.integration;
17: var hostAttributeTable = integration.host_attributes_table;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-30-2023 08:51 PM
import_set object refers to the import set that is being currently transformed.
I think your import set table (sys_import_set) doesn't have this field integrationProcessGr so it's breaking at that line
check these 4 lines
var integrationRun = import_set.integrationProcessGr.integration_run;
var sourceInstance = integrationRun.implementation;
var integration = integrationRun.implementation.integration;
var hostAttributeTable = integration.host_attributes_table;
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 10:25 AM - edited 12-01-2023 10:43 AM
I have checked the OnBefore script properties and it shows that it is in there. For some reason my import_set class is coming in as undefined. I don't see this field in any other integration like rapid7. I am following the Vulnerability Response Integration guide but it looks like it is incorrect.
https://docs.servicenow.com/bundle/vancouver-integrate-applications/page/script/server-scripting/ref...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 11:20 AM
Since the property is not in the import_set class, where can I get the integrationRun sys_id in the OnBefore script?
This script is in the transformation mapping.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-03-2023 07:33 AM
HI @sumanth maram ,
I trust you are doing great.
Here's a revised version of your script with added checks and comments for better understanding.
(function runTransformScript(source, map, log, target, import_set /* Added import_set as a parameter */) {
// Check if import_set is defined
if (!import_set || !import_set.integrationProcessGr) {
log.error("import_set is undefined or integrationProcessGr is not available.");
return; // Exit the script if import_set is not properly defined
}
// Handle re-opened VIs
if (action == "update" && target.state == 3) {
// We are re-opening
var now = new GlideDateTime();
target.state = '1';
target.substate = '';
target.reopened = true;
target.last_opened = now.getDisplayValue();
}
//populate ci
var integrationRun = import_set.integrationProcessGr.integration_run;
var sourceInstance = integrationRun.implementation;
var integration = integrationRun.implementation.integration;
var hostAttributeTable = integration.host_attributes_table;
target.integration = integration;
target.integration_instance = sourceInstance;
target.integration_run = integrationRun;
var host = {};
for (var key in source)
if (key.startsWith("u_"))
host[key.substring(2).toUpperCase()] = source.getValue(key);
host["MAC_ADDRESS"] = source.getValue("u_mac_address");
host["HOST_NAME"] = source.getValue("u_host_name");
host["ID"] = source.getValue("u_dev_id");
var hostInfoGR = new sn_vul.HostUtil().initializeHostAttributeRecord(host, hostAttributeTable);
var matchResult = new sn_vul.ImportHost().hostImport(
integrationRun.implementation, host, "ID", integrationRun,
false, false, source.getValue('u_last_assessed'), null, null, hostInfoGR);
if (matchResult.insert)
source.ci_operation = "inserted";
else
source.ci_operation = "updated";
target.cmdb_ci = matchResult.sys_id;
target.src_ci = matchResult.disc_item_id;
})(source, map, log, target, import_set); // Pass import_set when calling the function
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi