OnAfter Script - Transform Map

Ruchi19
Mega Contributor

I have the transform Map which has Onstart, Onbefore and Onafter script.

In Onbefore script, I am checking for 2 conditions. If the conditions are met , Ignore the record.
The issue is Ignore works but the same record goes through processing in Onafter script.
I want to ignore it completely if it is matches the condition in Onbefore script.

Do I have to write the same logic as onbefore in onafter script too?




Onbefore script:
if(source.u_user_id == "" || source.u_user_id == null)
ignore=true;
else {
//bypass the user if the bypass flag is on
var checkflag = new GlideRecord('sys_user');
checkflag.addQuery('user_name',source.u_user_id);
checkflag.query();

if(checkflag.next()) {
var bypass = checkflag.u_bypass_user_profile_updates;
if(bypass) {
ignore=true;
}
}
}

2 REPLIES 2

tony_fugere
Mega Guru

One could interpret this as a "bug" or a "feature" of ServiceNow. I can agree with you here that the onAfter should not fire if onBefore sets it to ignore=true. You can work around it though:

Centralize your condition into a single function using On-demand Functions and then call it from your onBefore AND your onAfter. This would allow you to have a single place to change the condition(s) around.


Dominik Simunek
Tera Guru

I had the same issue however my validation logic was quite time consuming so that I wanted to prevent to execute it twice. So I was somehow thinking about how to send the information about invalid record to the onAfter script and I resolved it by creating custom variable in onBefore transform script (or in Script of Transform Map record) and I set it to true if onAfter script should be ignored, otherwise to false. Then in onAfter script I checked this variable and it was available there. This was tested on Fuji release.



On Before transform script:


if (/* validation code indicates invalid values */) {


  ignore = true;


  var wasIgnored = true;


} else {


  var wasIgnored = false;


}



On After transform script:


if (wasIgnored == false) {


  // run my on after code


}