Java "Includes" Not Working

Post Reply
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Java "Includes" Not Working

Post by rgpepper »

What am I missing? I'm attempting to stash a piece of text in the JobState to be retrieved later by a configurator.

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{

var jobName = job.getNameProper();

job.log(1, jobName);

if (jobName.includes ("PORTFL")) {
job.setJobState("PORTFL");
}

if (jobName.includes ("PORTFD")) {
job.setJobState("PORTFD");
}

if (jobName.includes ("LANDFL")) {
job.setJobState("LANDFL");
}

if (jobName.includes ("LANDFD")) {
job.setJobState("LANDFD");
}

job.sendToSingle( job.getPath());

}

I get an error at whatever line number the second "if" is on, so presumably my first if statement is incorrect, though I don't get any syntax errors when saving the script
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Java "Includes" Not Working

Post by gabrielp »

Take my comments with a grain of salt because I don't have access to Switch Scripter any longer.

1) You have an extra space before you invoke the .includes method

Code: Select all

if (jobName.includes ("PORTFL")) {
    job.setJobState("PORTFL");
}
should be

Code: Select all

if (jobName.includes("PORTFL")) {
    job.setJobState("PORTFL");
}
2) MDN says that String.prototype.includes() was introduced for ECMAScript 2015. Last I heard SwitchScriper uses a modified ES4, so this may not be available https://developer.mozilla.org/en-US/doc ... g/includes

You can see what is available in the Scripter string here https://www.enfocus.com/manuals/Develop ... tring.html

3) I think the way to do this in Switch would be to use indexOf on a particular pattern or substring like so:

Code: Select all

function jobArrived( s : Switch, job : Job )
{

    var jobName = job.getNameProper();

    job.log(1, jobName);

    if (jobName.indexOf("PORTFL") !== -1) {
        job.setJobState("PORTFL");
    }
    /* etc */
    
    job.sendToSingle( job.getPath());

}
4) I would use a private data key for storing this instead of jobstate because it provides more context about what is stored there and its less likely to get overwritten by something else (including a folder with JS set).
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: Java "Includes" Not Working

Post by rgpepper »

Thanks! Turns out I did have to use "IndexOf", and I'm golden!
Post Reply