Using JS To Add Email Addresses

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

Using JS To Add Email Addresses

Post by rgpepper »

Rather than my spaghetti folder configuration to do this, I decided to try and conquer a script. The purpose of the script is to grab data from 1 (of several) fields submitted to a Submit Point by a Connector (XML job ticket), and use if/then's to determine which additional email address should be added to the job, which then simply goes to a Email Send configurator to send it.
What it's doing is nothing - it comes into the flow in a Submit point and just stops, not being acted on by the script which is the very next configurator in the flow - no errors, no nothing. I have a Flow element properties named "DatasetName" with just a single-line text for the Inline editor, and the "Default" property set to "Submit". I know it's hard to troubleshoot with this few of details, but first-off, does my script look right to perform this action?
Here's a truncated version of my code:

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

var dataset = job.getDataset( s.getPropertyValue( "DatasetName"));
var xmlFilePath = dataset.getPath();
var xmlObj = new Document( xmlFilePath);
if (xmlObj.isWellFormed() == false) {
job.fail( "Bad XML");
return;
}

var mgrName = xmlObj.evalToString( "/field-list/field[6]/value");
job.log(1, mgrName);

if (mgrName === "Dr Pepper") {
job.addEmailAddress("info@mycompany.biz");
}
}
freddyp
Advanced member
Posts: 1009
Joined: Thu Feb 09, 2012 3:53 pm

Re: Using JS To Add Email Addresses

Post by freddyp »

You are on the right track. The only thing you still have to do is to send the job to the output folder. In your case a single output connection will be enough, so in the properties of the script you define it as having just one output connection and the end of the jobArrived function you add:

Code: Select all

job.sendToSingle( job.getPath());
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: Using JS To Add Email Addresses

Post by LasseThid »

For added readability and flexibility you could replace "/field-list/field[6]/value" with "//field[tag='Manager']/value" or whatever the field tag is that you want to use.

It's much easier to understand what //field[tag='Gramvikt']/value is than what /field-list/field[2]/field-list/field[1]/field-list/field/field-list/field/value is... :lol:

Also, in case you make changes to your submit point in the future "/field-list/field[6]/value" may give you incorrect data while "//field[tag='Manager']/value" will always give you the same data... :!:

Skärmavbild 2019-10-11 kl. 15.15.50.png
Skärmavbild 2019-10-11 kl. 15.15.50.png (16.41 KiB) Viewed 13111 times
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: Using JS To Add Email Addresses

Post by rgpepper »

Thank You both Gentlemen! I knew about "It's much easier to understand what //field[tag='Gramvikt']/value is than what /field-list/field[2]/field-list/field[1]/field-list/field/field-list/field/value is... " but never having dealt with it in the context of a script and not sure how "deep" I had to define the path, I went with the "long route". And Freddie, I just overlooked that last line of code in the video that Frank sent me, you did your part! I just biffed it.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: Using JS To Add Email Addresses

Post by rgpepper »

freddyp wrote: Fri Oct 11, 2019 10:36 am You are on the right track. The only thing you still have to do is to send the job to the output folder. In your case a single output connection will be enough, so in the properties of the script you define it as having just one output connection and the end of the jobArrived function you add:

Code: Select all

job.sendToSingle( job.getPath());
Unfortunately, this isn't working. Not sure why I thought it did initially but I suspect I was confused. It doesn't error, the job passes through, the other email addresses I'm including statically are the only addresses that receive the email.
Here's my current code:

// 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 dataset = job.getDataset( s.getPropertyValue( "DatasetName"));
var xmlFilePath = dataset.getPath();
var xmlObj = new Document( xmlFilePath);
if (xmlObj.isWellFormed() == false) {
job.fail( "Bad XML");
return;
}

var mgrName = xmlObj.evalToString("field[tag='Salesperson/CSA']/value"); // ( "/field-list/field[6]/value");

job.log(1, mgrName);

if (mgrName === "Jenna") {
job.addEmailAddress("jrsmi@domain.com");
}

if (mgrName === "Randy_Connie") {
job.addEmailAddress("rpkroen@domain.com;clzimmerm@domain.com");
}

if (mgrName === "Joe") {
job.addEmailAddress("jdfergus@domain.com");
}

if (mgrName === "Dave_Marcus") {
job.addEmailAddress("ddnazaren@domain.com;mjduen@domain.com");
}

if (mgrName === "Walters") {
job.addEmailAddress("rgpepper@domain.com");
}

if (mgrName === "Annette") {
job.addEmailAddress("alboe@domain.com;mjschmi@domain.com");
}

if (mgrName === "Robin") {
job.addEmailAddress("rmeva@domain.com;mjschmi@domain.com");
}

if (mgrName === "Eddie") {
job.addEmailAddress("eafah@domain.com;mjschmi@domain.com");
}

if (mgrName === "Ellen") {
job.addEmailAddress("emhans@domain.com;mjschmi@domain.com");
}

if (mgrName === "Justin") {
job.addEmailAddress("jgsh@domain.com;mjschmi@domain.com");
}
if (mgrName === "Ellen") {
job.addEmailAddress("emhans@domain.com;mjschmi@domain.com");
}

if (mgrName === "Dave_Laurie") {
job.addEmailAddress("dtbruggem@domain.com;lasha@domain.com");
}
job.sendToSingle( job.getPath());

}
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Using JS To Add Email Addresses

Post by gabrielp »

What does `mgrName` log as?
tag='Salesperson/CSA']/value"
Perhaps this slash is causing problems. You'll have to do some debugging to see if the dataset is there and why the xpath isn't returning the right thing.

Honestly, I wouldn't even do this metadata lookup and selection within the script. It means you'll have to modify your code if you change this reference in the future. Consider setting this value as a property and getting it like this:

Code: Select all

s.getPropertyValue("ManagerName")
Within the script configuration, the user can select the dataset and xpath to the manager name and set it as a property of this script. Switch has a nice interface for browsing and selecting dataset values. The script can then be simplified to map the name to emails.

Then, in the future you could further optimize this script (by replacing it...) with https://www.enfocus.com/en/appstore/product/csv-lookup which would be way more maintainable going forward.

In the future, try to use

Code: Select all

[code]
blocks as it makes it way easier to read.
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: Using JS To Add Email Addresses

Post by rgpepper »

Call me a hack, I'm new to JS (worse yet, within Switch), this worked for me - a blend of observations by Gabriel. I used a previous folder in the flow to attach the field I needed as the JobState like so:

'[Metadata.TextIndexed:Path="/field-list/field[6]/value",Dataset="Submit",Model="XML"]' - it only works with the single quotes around this bit.

Then:

Code: Select all

// 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 dataset = job.getDataset( s.getPropertyValue( "DatasetName"));
	var xmlFilePath = dataset.getPath();
	var xmlObj = new Document( xmlFilePath);
	if (xmlObj.isWellFormed() == false) {
		job.fail( "Bad XML");
		return;
	}
	
	var mgrName = xmlObj.evalToString(job.getJobState());
	
	job.log(1, mgrName);
	
Post Reply