Page 1 of 1

How to create a dataset

Posted: Wed Mar 13, 2019 6:10 pm
by apietrocini
Can someone put on the right path... I'm trying to create a script that will build or create a dataset... This is what I "think" needs done, just not sure on the proper execution...

function jobArrived( s: Switch, job: job )

var theDatasetName = s.getPropertyValue("DataSetName");
var theKey = s.getPropertyValue("Key");
var theValue = s.getPropertyValue("Value");

I would guess that I need to map the key and value to the dataset, just not clear on the proper way to do that...

Thanks in advance,

Anthony

Re: How to create a dataset

Posted: Wed Mar 13, 2019 6:23 pm
by gabrielp
For simple key value pairs its much easier to write to private data -- but here is an example of writing to a dataset:
- https://github.com/open-automation/swit ... #L324-L367

Re: How to create a dataset

Posted: Wed Mar 13, 2019 6:52 pm
by apietrocini
Thank you!

Re: How to create a dataset

Posted: Wed Mar 13, 2019 9:31 pm
by apietrocini
Ok... This may be a dumb question, but is there a way to attach a complete dataset to one private data key...?

Re: How to create a dataset

Posted: Thu Mar 14, 2019 12:40 am
by gabrielp
apietrocini wrote: Wed Mar 13, 2019 9:31 pm Ok... This may be a dumb question, but is there a way to attach a complete dataset to one private data key...?
No dumb questions here. I haven't used Switch in a little while but here's my take on this.

The private data methods (setPrivateData, getPrivateData) allow you to set and get strings (https://www.enfocus.com/manuals/Develop ... class.html). They usually aren't used to access sets of data -- more often for simple key/value pairs (example of two private data keys: MyJobNumber => "123456", MyCustomer => "Acme Corp"). But in that example, we have two related pieces of data saved as two keys (MyJobNumber and MyCustomer), so one might say that is a "data set". In fact, the "multiple result value" option of the script I gave you encodes a result of data into many PD keys (e.g. row1, row2, row3).

But a string is a string, so you could encode some form of dataset as a string, set it, then get it again. For example, let's say you had the JSON object:

Code: Select all

{
    "this": "that",
    "these": "those"
}
You could certainly set some private data with that encoded as a string (e.g. MyObject => '{"this":"that", "these": "'those"}'). But now you need to use scripting to parse out the contents again -- which could work fine for your use case. But, your Switch users will see confusing encoded strings within that single PD key -- which could be a problem. So yes, you can but its hard to deal with.

But Switch's first-class handling of Datasets is better suited for this job -- if you can get past the fact that they are a little harder to use (harder then setting private data anyways). You can do it a few ways -- but the most straightforward is to create an XML structure of your document and use that.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<this>that</this>
<these>those</these>
Like private data, you retrieve the data with a key (e.g. MyDataset) but now you don't have to worry about decoding/parsing out bits of data like you would previously. You can navigate through the data in Switch with an expression like this:

Code: Select all

[Metadata.Text:Path="/[tag='this']/value",Dataset="MyDataset",Model="XML"]
Personally, I relied on (some would say abused) private data in most of my scripts. For a small number of data items, it was always very easy for users to select a few keys and for me to write those into private data. But if you have a large document then the Dataset is the way to go.

Re: How to create a dataset

Posted: Thu Mar 14, 2019 9:30 am
by jan_suhr
There is an app for that :-)

With the App "Make XML" you can create your own dataset form variables in Switch or write your own values.

The App can save this as an XML file or as a Dataset.

https://www.enfocus.com/en/appstore/product/make-xml

Re: How to create a dataset

Posted: Fri Mar 22, 2019 11:20 pm
by apietrocini
Thank you!!! I got it to work!

Re: How to create a dataset

Posted: Sat Mar 23, 2019 10:23 am
by risha
Thanks for sharing great information on data set, keep sharing.

Re: How to create a dataset

Posted: Thu Mar 28, 2019 10:36 am
by Nayna
Thanks for sharing information.