Import set is Ignoring all record with OnBefore script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 09:13 AM
We have integrated with a third part tool where the query push the data to Servicenow. The query has been mapped with import set table. The data comes to to import set table and via transform map, it pushes the data to target table with serial number as coalescing field. This was creating duplicates and we have applied onbefore script with the below but this is just ignoring the records and not updating them or inserting. Please advise. @CMDB
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 05:05 PM
This is kinda' expected since the script is executing instruction ignore = true;.
If you want to see what has been updated, what you could do is to create two totally separate Transform Maps:
- one helper that would (only) be used as parameter (2) to identifyAndReconcile - this would contain all the field maps, like name, serial, etc and would basically be disabled/not-active.
- another that would do the actual import, but this time it would have just two fields mapped:
- one scripted, targeting field sys_id, marked as coalesce (!) and which when calling identifyAndReconcile would return the identified CIs sys_id:
(function runTransformScript (source, map, log) {
var fieldsMap = getOtherTransformMap();
var cmdbUtil = new CMDBTransformUtil();
var payload = cmdbUtil.getPayload(source, fieldsMap, log);
// Set the payload that has been generated above based on the Helper Transform Map
cmdbUtil.setPayload(payload);
// This will create or update a CI
cmdbUtil.identifyAndReconcile(source, map, log);
if (cmdbUtil.hasError()) {
var errorMessage = cmdbUtil.getError();
log.error(errorMessage);
// Some error occured, so abort the current CI's/record's tranformation
ignore = true;
}
else {
log.info('IE Output Payload: ' + cmdbUtil.getOutputPayload());
log.info('Imported CI: ' + cmdbUtil.getOutputRecordSysId());
// A CI/record has been created, so return its sys_id
return cmdbUtil.getOutputRecordSysId();
}
// Load the Helper Transform Map
function getOtherTransformMap () {
var $gr = new GlideRecord('sys_transform_map');
return $gr.get('<sys_id of the "helper" Transform Map>') ? $gr : undefined;
}
})(source, map, log);​
- any other field that would result in an update - it may be that this 2nd field is not even necessary.
Using such a setup should make all transformed rows either show the identified updated or created CI, or that it produced an error - in the 2nd active Transform Map.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2023 05:34 PM
Here's the "Helper"/"Payload" Transform map:
Here's the "actual" transform map:
The coalesce script is:
answer = (function transformEntry (source, map, log, sourceName) {
var payloadMap = getPayloadMap('9352fd7b97b2b910594ffbc71153af27');
var cmdbUtil = new CMDBTransformUtil();
cmdbUtil.setDataSource(sourceName);
var payload = cmdbUtil.getPayload(source, payloadMap, log);
payload.items[0].internal_id = '' + source.u_correlation_id;
payload.items[0].sys_object_source_info = { 'source_name': sourceName, 'source_native_key': '' + source.u_correlation_id, };
cmdbUtil.setPayload(payload);
cmdbUtil.identifyAndReconcileEnhanced(source, map, log);
if (cmdbUtil.hasError()) {
log.error(cmdbUtil.getError());
ignore = true;
error = true;
return -1;
}
else {
gs.info('IE Output Payload: ' + JSON.stringify(JSON.parse(cmdbUtil.getOutputPayload()), null, '\t'));
gs.info('Imported CI: ' + cmdbUtil.getOutputRecordSysId());
gs.info('cmdbUtil.getOutputRecordSysId(): ' + cmdbUtil.getOutputRecordSysId());
return cmdbUtil.getOutputRecordSysId();
}
function getPayloadMap (uniqueValue) {
var $gr = new GlideRecord('sys_transform_map');
return $gr.get(uniqueValue) ? $gr : undefined;
}
})(source, map, log, 'OCSImport');
where 9352fd7b97b2b910594ffbc71153af27 is the unique id of the "Helper"/Payload Transform Map.
Under these circumstances, after I do that transform, I get a reference to the created CI in the logs: