Transform Map in Synchronous Mode - Coalesce Field Script Called Twice

Martin76
Kilo Expert

I have noticed a strange behaviour in executing Transform Maps in synchronous mode (i.e. via background script or web service).

Normally (in asynchronous mode), transform scripts are executed in this order:

  1. onStart
  2. field source script for coalesce fields
  3. onBefore
  4. other source scripts
  5. transform script
  6. ...

However, with synchronous mode, field source scripts for coalesce fields is called twice:

  1. field source script for coalesce fields (extra call)
  2. onStart
  3. field source script for coalesce fields
  4. onBefore
  5. other source scripts
  6. transform script
  7. ...

Of course, if your coalesce field source script relies on anything declared in an onStart script, you are out of luck.

Is it a bug or a feature?

10 REPLIES 10

Hi Ankur,

unfortunately, adding the numeric value only confirms what I was saying.

I have a transform map with a single (scripted) coalesce field and a signle onStart script.

onStart:

gs.log('Transform MAP onStart called at ' + new GlideDateTime().getNumericValue() + '. I am also defining a new function hello() which always returns true');
function hello() {
return true;
}

scripted coalesce field:

answer = (function transformEntry(source) {
gs.log('Transform MAP coalesce field map (script) called at ' + new GlideDateTime().getNumericValue() + '. type of hello is: ' + typeof hello + ', should be function');
// Add your code here
return true; // return the value to be put into the target field

})(source);

When I run in a background script (or automated test):


var gr = new GlideRecord("imp_user");
gr.initialize();
gr.email = "blablabla@blablabla.bla";
gr.first_name = "First";
gr.last_name="Last";
gr.insert();

 

I get:

*** Script: 16
*** Script: Transform MAP coalesce field map (script) called at 1567633103785. type of hello is: undefined, should be function
*** Script: Transform MAP onStart called at 1567633103788. I am also defining a new function hello() which always returns true
*** Script: Transform MAP coalesce field map (script) called at 1567633103789. type of hello is: function, should be function

As you can see, the coalesce field script is evaluated first - before the onStart script is executed. And it does not see the hello() function because it hasn't been declared yet. Just then the onStart script is run. Finally, the coalesce script is executed again.

So in my opinion, the first execution of the coalesce field script is redundant and even worse, unwanted.

Are you able to reproduce the issue with the information provided?
Thank you a lot

Martin

Community Alums
Not applicable

Hi Martin,

I know this thread is about a year old but I'm experiencing this situation in Paris.  Did you find a solution for this?

 

Thanks,

Mike

 

Hi Mike,

sad to hear that this hasn't been fixed yet.

 

As I hadn't found help on the ServiceNow community, I contacted the support through their "Hi" portal last year. The assigned engineer was able to reproduce the issue and created a problem ticket for that: PRB1368845. However, I'm unable to find that ticket anymore, not sure why. Perhaps I (or you) would have to contact them again and ask what the status is.

Nevertheless, I've found a workaround although I don't particularly like it:

You can take the advantage of all of the involved scripts sharing the same variable scope and define a variable in your onStart script, such as

var onStartCalled = true;

Then simply wrap your field source script in something like

if (onStartCalled) {
   answer = …
}

This does the trick.

Best,
Martin

 

Community Alums
Not applicable

Thanks Martin for the information.

I used your workaround but for some reason, the import row is still being ignored and the field that I expect to be updated is not getting updated.

All I'm doing is coalescing on the sys_id using a source script and want to update a date but the import set is being ignored and I believe it is the first Field Map call that is executed before onStart that's causing the row to be ignored.

 

If you have any other ideas, I'd love to hear them.  Thanks!

 

Sorry for the delay.

Since the issue only occurs in synchronous mode, may I ask whether you managed to get the same code work in asynchronous mode? (Just for test purposes)

There are some ways how to achieve this, perhaps you can have a look at this article