Calling package script functions from script expressions

Post Reply
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

Calling package script functions from script expressions

Post 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
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Calling package script functions from script expressions

Post by dkelly »

Lookup globalData in scripting manual
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

Calling package script functions from script expressions

Post 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
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Calling package script functions from script expressions

Post 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" );


ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

Calling package script functions from script expressions

Post 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
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

Calling package script functions from script expressions

Post 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;
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Calling package script functions from script expressions

Post 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


dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Calling package script functions from script expressions

Post 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.
ArielRauch
Advanced member
Posts: 231
Joined: Thu Aug 07, 2014 10:04 am

Calling package script functions from script expressions

Post 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?
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Calling package script functions from script expressions

Post by dkelly »

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


no
Post Reply