Page 1 of 1
Declare string variables, get a variable from private data, and join
Posted: Tue Jun 04, 2024 3:33 am
by cwswitch
I am very behind with converting to Node
Can some kind person help me see how I would do the following in Node, please?
Code: Select all
var OpenSection = '[Metadata.Text:Dataset="Xml",Model="XML",Path="/Job/Something[';
var SectionKey = job.getVariableAsString("[Job.PrivateData:Key=\"sKey\"]");
var CloseSection = ']/Key"]';
var SectionData = OpenSection+SectionKey+CloseSection;
var SectionFinder = job.getVariableAsString(SectionData);
SectionFinder;
It basically writes a path from
/Job/Something + A private data value + the rest of the path
I hope that seeing an example will help me greatly as I'm up against too many walls at once right now.
Issues I face.
1) my first Node so getting started at all
2) getVariableAsString is not supported and I do not see much (I can understand) that helps me
3) I begin to find it hard to see my screen through my tears
Re: Declare string variables, get a variable from private data, and join
Posted: Wed Jun 05, 2024 7:19 am
by cwswitch
I have got this far today by following the tutorial then reading a lot. I am one step from glory, so still looking for help
Code: Select all
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
let a:string = '[Metadata.Text:Dataset="Xml",Model="XML",Path="/Job/Something[';
let b:string = (await job.getPrivateData("sKey"));
let c:string = ']/Key"]';
let d:string = a+b+c;
let SectionFinder = ****I'm Stuck Here****
await job.log(LogLevel.Info, `a is '${a}'`);
await job.log(LogLevel.Info, `b is '${b}'`);
await job.log(LogLevel.Info, `c is '${c}'`);
await job.log(LogLevel.Info, `d is '${d}'`);
}
I'm stuck on the bit that in Legacy was
Code: Select all
var SectionFinder = job.getVariableAsString(d);
SectionFinder;
Which would ask Switch to give me the value at the path "d"
Re: Declare string variables, get a variable from private data, and join
Posted: Wed Jun 05, 2024 9:06 pm
by tdeschampsBluewest
Hi there,
Sadly there is no replacement for "job.getVariableAsString" in nodeJS, and we lost some "easy" way to grab info from switch.
To gather data trough an Xpath, you'll need this
function :
Code: Select all
async function getXpath(job: Job, dataset: string, xpath: string): Promise<string> {
let result: string | number | boolean;
try {
const datasetPath = await job.getDataset(dataset, AccessLevel.ReadOnly);
const XML = XmlDocument.open(datasetPath);
const NSmap = XML.getDefaultNSMap();
const evaluation = XML.evaluate(`string(${xpath})`, NSmap);
result = evaluation;
} catch (e) {
result = e.message;
}
return result.toString();
}
I also strongly suggest you to use template string with "backtick", when you need to "insert" something in a string, it's way more readable :
Code: Select all
const sectionKey = "something"
const xPath = `/Job/Something[${sectionKey}]/Key`
Everything put together will result in something like this :
Code: Select all
async function getXpath(job: Job, dataset: string, xpath: string): Promise<string> {
let result: string | number | boolean;
try {
const datasetPath = await job.getDataset(dataset, AccessLevel.ReadOnly);
const XML = XmlDocument.open(datasetPath);
const NSmap = XML.getDefaultNSMap();
const evaluation = XML.evaluate(`string(${xpath})`, NSmap);
result = evaluation;
} catch (e) {
result = e.message;
}
return result.toString();
}
async function calculateScriptExpression(s: Switch, flowElement: FlowElement, job: Job): Promise<string> {
const dataset = "Xml";
const sectionKey = await job.getPrivateData('\"sKey\"]');
const xPath = `/Job/Something[${sectionKey}]/Key`
const result = await getXpath(job, dataset, xPath);
return result;
}
Let us know if it work!
Re: Declare string variables, get a variable from private data, and join
Posted: Thu Jun 06, 2024 9:20 am
by cwswitch
A few things tdeschampsBluewest,
1)
many many thanks for the notes and examples! I could hug you. Some things are beginning to make sense.
2) it did not work out of the box, which is good as it has left me learning.
3) I was able to make a script expression work with an app, which was cool.
4) I am still unable to bring things together to make an actual script. I believe I need an entry point. I cannot see how to call these functions from elsewhere in the script or how to arrange things at all.
I got lost down a few holes today and progressed some more training, but it still did not click.
5) Thanks again

Re: Declare string variables, get a variable from private data, and join
Posted: Thu Jun 06, 2024 3:49 pm
by tdeschampsBluewest
Hi again!
For an app, the entrypoint is not the same, and the output should/will be different.
In a script expression, you always "return" something.
In an App, you'll need to store the result in a way switch will be able to read it afterwhile.
Code: Select all
async function getXpath(job: Job, dataset: string, xpath: string): Promise<string> {
let result: string | number | boolean;
try {
const datasetPath = await job.getDataset(dataset, AccessLevel.ReadOnly);
const XML = XmlDocument.open(datasetPath);
const NSmap = XML.getDefaultNSMap();
const evaluation = XML.evaluate(`string(${xpath})`, NSmap);
result = evaluation;
} catch (e) {
result = e.message;
}
return result.toString();
}
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
const dataset = await flowElement.getPropertyStringValue("datasetName"); //"Xml"
const sectionKey = await job.getPrivateData('\"sKey\"]');
const xPath = `/Job/Something[${sectionKey}]/Key`;
const result = await getXpath(job, dataset, xPath);
const newPrivateDataName = await flowElement.getPropertyStringValue("newPrivateDataName") as string; //"My Private data"
await job.setPrivateData(newPrivateDataName, result);
await job.sendToSingle();
}
Do not forget to "transpile" your Typescript, if you work with an app. The command line should be :
Code: Select all
SwitchScriptTool.exe --transpile PathToYourTypscriptFolder
If you work with VS code, you can automate this, with runOnSave (emerald walk) addon and by adding thoses value in your settings.json :
Code: Select all
"emeraldwalk.runonsave": {
"autoClearConsole": false,
"commands": [
{
"match": "\\.ts$",
"cmd": "cd ${fileDirname}"
},
{
"match": "\\.ts$",
"cmd": "SwitchScriptTool.exe --transpile ${fileDirname}"
}
]
},
Re: Declare string variables, get a variable from private data, and join
Posted: Fri Jun 07, 2024 1:25 am
by cwswitch
Awesome tips. Thanks so much for the hand holding

very much appreciate your time.
I will go into a darkened room for a while and let you know how I go...
Re: Declare string variables, get a variable from private data, and join
Posted: Fri Jun 07, 2024 8:02 am
by cwswitch
Party time at my house!
In a way you did hand me the solution, thanks for that.
I did though do lots more in the background to get this where it needed to be, and mainly, I understood the code and was able to work with it.
Many thanks for the help here.