Transform Map OnBefore - Update Existing Inc vs Insert if Closed

MarkyMark1
Tera Expert

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);
1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

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.

View solution in original post

9 REPLIES 9

Thanks for the suggestion but like from my original script it still will update and reopen a closed matching incident.  😞

No it will not update if you mention action =insert. It will only insert a record. Kindly check once.

Alternatively, you can do it like this. Add ignore=true so that the record is skipped and then insert a record from script.

(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()){				
		ignore=true;
                 grIncident.initialize();
                  //ADD YOUR FIELDS HERE
                 grIncident.insert();		
	}	
	
})(source, map, log, target);

RAHUL Khanna1
Mega Guru

1) Can you try some thing like this: 

onBefoe Script: 

if(action== "update"){

if(source.active == true){

action = "insert"; 

}

}

I wish this would have worked as its much more compact but I never could get it to work when using action = 'insert'.  thanks