Page 1 of 1

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 2:02 pm
by ArielRauch
Hi,

I have built a nice little lookup table utility which reads values into drop down lists from txt files.

As I will use this from many different locations I was wondering whether there is a way to "store it in the system" so that I only have to call a function to receive the list.



Thank you



Ariel

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 3:38 pm
by dkelly
Lookup globalData in scripting manual

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 4:31 pm
by ArielRauch
Thanks for the hint.

I looked at it but I am not sure that I understand how to upload functions as global data.



Do have a code sample for it?



Thanks



Ariel

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 4:54 pm
by dkelly
Using scripting you can set a value



s.setGlobalData( "uniqueName", "tagName", "value", true );



and read it back



var value = s.getGlobalData("uniqueName", "tagName" );



Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 5:08 pm
by ArielRauch
2 questions:



1. Where do I call the initialization of the global data



2. Can I store only flat strings or also functions or lists



Thanks



Ariel

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 5:59 pm
by ArielRauch
Could you please have a look at the code - something I miss. It stopped working after I added the globalData parts



Thanks



Ariel



var tblPath = "d:/Switch/configurations/tables/";

var tblName = "product_types";

var tblExt = ".tbl";

var tblFile = new File(tblPath+tblName+tblExt);

var list = new Array;

tblFile.open(File.ReadOnly);

var hdrline = tblFile.readLine();

do {

var line = tblFile.readLine();

var cols = line.split(",");

var pType = s.getGlobalData( "c-copy.co.il", cols[1] );

if ( typeOf(pType) != "undefined" ) {

s.setGlobalData("c-copy.co.il",cols[1], pType + ";" + cols[0]);

}

else {

s.setGlobalData("c-copy.co.il",cols[1],cols[0]);

}

list.push(cols[0]);

} while (!tblFile.eof);

tblFile.close();

list;

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 7:04 pm
by dkelly
ArielRauch wrote: 1. Where do I call the initialization of the global data




write a separate flow to initialize the values



ArielRauch wrote: 2. Can I store only flat strings or also functions or lists


any JS object can be converted to/from a string



Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 7:12 pm
by dkelly


var tblPath = "d:/Switch/configurations/tables/";

var tblName = "product_types";

var tblExt = ".tbl";

var tblFile = new File(tblPath+tblName+tblExt);

var list = new Array;

tblFile.open(File.ReadOnly);

var hdrline = tblFile.readLine();

do {

var line = tblFile.readLine();

var cols = line.split(",");

var pType = s.getGlobalData( "c-copy.co.il", cols[1] );

if ( pType.length != 0 ) {

s.setGlobalData("c-copy.co.il",cols[1], pType + ";" + cols[0]);

} else {

s.setGlobalData("c-copy.co.il",cols[1],cols[0]);

}

list.push(cols[0]);

} while (!tblFile.eof);

tblFile.close();

list;





typeof was used incorrectly and getGlobalData returns empty string if not defined.

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 7:21 pm
by ArielRauch
You are the man! Thank you very much.



Due to your previous answer I can build a initialization flow and load all the lookup table in memory.

In the script expression of the metadata field I only need to catch and decode the string.



Is it possible to store a routine in global data and to execute it dynamically?

Calling package script functions from script expressions

Posted: Thu Sep 11, 2014 8:02 pm
by dkelly
ArielRauch wrote: Is it possible to store a routine in global data and to execute it dynamically?


no