- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2020 01:07 PM
I have this transform map that updates or inserts records into the incident table coalescing off an id field. Unfortunately, sometimes the col_id value passed in matches an incident where the state is closed. If the state is closed I want it create/insert a new incident and not re-open it and update it.
So I'm trying to get an onBefore script to work but it doesn't seem to want to work. Any ideas on how to make this happen?
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var grIncident = new GlideRecord('incident');
grIncident.addQuery('correlation_id', source.correlation_id); //correlation_id
grIncident.addQuery('state', '!=', 7);
grIncident.query();
//matching incident record found. will update
if (grIncident.next()){
}
else {
//no matching record found or if found was closed. create/insert new record
action = "insert";
}
})(source, map, log, target);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2020 01:13 PM
You can do some improvements to the script
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if (target.active!=true && action == "update")
action = "insert";
})(source, map, log, target);
If this doesnt work, you will have to use ignore to ignore the current update and then insert the record using script for ex
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if (target.active!=true && action == "update")
{
ignore = "true";
var gr = new GlideRecord('incident');
gr.initialize();
gr.correlation_id = source.correlation_id;
// Add additional fields here
gr.insert();
}
})(source, map, log, target);
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2020 01:13 PM
You can do some improvements to the script
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if (target.active!=true && action == "update")
action = "insert";
})(source, map, log, target);
If this doesnt work, you will have to use ignore to ignore the current update and then insert the record using script for ex
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
if (target.active!=true && action == "update")
{
ignore = "true";
var gr = new GlideRecord('incident');
gr.initialize();
gr.correlation_id = source.correlation_id;
// Add additional fields here
gr.insert();
}
})(source, map, log, target);
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2020 07:26 AM
Thanks for the suggestions. The second suggestion does work to a degree but when the if statement evaluates to false (where the coalesce field has no match of any kind so create/insert new incident) then this script creates a new incident and then the transform creates another incident. Now I get two new ones.
if (target.active!=true && action == "update")
{
ignore = "true";
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2020 08:19 AM
Actually it looks like my closing } was not in the right place after following your second suggestion so the create new incident code was outside the if {} which would allow creating of a new incident in the onBefore and the transform. Appears things are working as desired now once I fixed that mistake. Thanks so much!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2020 07:48 AM
Try like this.
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
var grIncident = new GlideRecord('incident');
grIncident.addQuery('correlation_id', source.correlation_id); //correlation_id
grIncident.addQuery('state',7);
grIncident.query();
//matching incident record found. set action to insert to create new record
if (grIncident.next()){
action="insert";
}
})(source, map, log, target);
Mark the comment as a correct answer and also helpful if it helps to solve the problem.