Can you create a 'global' variable within the Transform Map?

tobrien
Kilo Guru

Hi,

We're on Helsinki...

I just finished watching a video from ChuckT. from 2014 regarding the use of a 'global' variable within a Script Include. In my case I would like something very similar to assist in a Transform Map.

To borrow from the video -- is it possible to have, say --->

OnBefore:

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {

        //my Global variable??

        if (MyVAR == 'undefined')

                  MyVAR = 'some_original_thing';

        if (source.some_source_field != '')

                  MyVAR = source.some_source_field;

        else

                  source.some_source_field = MyVar;

       

//I am trying to 'recall' a value from a 'previous' row for subsequent rows that do not have that some_source_field set

})(source, map, log, target);

in the video it was stated that a variable not declared with 'var' will be 'global'   --- does this work for Transform Maps?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

Hi Anthony,



Yes, you can do this with a transform script. Declare it in an onStart script and it's available to other scripts within the transform process.


View solution in original post

8 REPLIES 8

You bet! Come find me. I'm in 2 days of pre-con training. Tuesday-Thursday I'll be in the CreatorCon Dev Hub most of the time. I've got live sessions on the developer theater from 12:40 to 1:20 Tue/Wed. I look forward to seeing you there!


This appears to be untrue in Jakarta. I have an onStart script which contains...

var count = 0;

My field map script contains 

count++;

I get the following log message: org.mozilla.javascript.EcmaError: "count" is not defined.

did you find any solution?

Transform Map onStart event scripts can no longer declare global variables

"Since Geneva, Transform Map 'onStart' event scripts can no longer declare global variables across the transform event scripts.
Objects that are initialized in OnStart events of Transform Map are not available further down in other events of mapping.
Imports fail with the following error"

 

We have to create global variables outside the function

"The recommended practice is to not use global variables inside transform map event scripts. If global variables are needed, they can be declared outside function template and passed in, and the previous template will have to be removed"

-Anders