Page 1 of 1

sendto - send file to the right connection

Posted: Sun Dec 28, 2014 8:16 pm
by cristalica
Hello everybody,



I'm new to Switch scripting and I'm trying to create a script which checks for file avalibity (in 2 Windows folders) and move on a metadata file to connection A or B.



So far I could get the check part working but I still struggle on how to send the file to Switch workflow folder A or B, depanding on scripts result.



The function I try to use is job.sendTo( );



Can somebody give me an idea how to use it to send the file to the next folder element?



Lets say we have a folder "Start" and it has a direct connection A and also a direct connection B. I want to be able to control wheter the file proceeds to folder A or B.



Thanks for any help,



Daniel Krueger

sendto - send file to the right connection

Posted: Sun Dec 28, 2014 10:42 pm
by gabrielp
cristalica wrote: I'm new to Switch scripting and I'm trying to create a script which checks for file avalibity
You may find that "Inject Job" will do this for you without any scripting efforts.



cristalica wrote: So far I could get the check part working but I still struggle on how to send the file to Switch workflow folder A or B, depanding on scripts result.


Take a look at the "Traffic Light" outgoing connection type:





You can specify to send out data or log files. Of those two types, you can specify them as success, warn, or error. So you have up to 6 outgoing connections where you can divert files based on script logic. It looks like this:

// Finish

job.sendToData(1, job.getPath());

job.sendToLog(1, some_other_file);

The first parameter is an integer saying what type of connection it is. 1 is success, 2 is warn and I'm pretty sure 3 is error. This works and I have done it but if your output files don't really represent this data model, it might be too messy to apply this to your script. Ie... if you have two files that are successful and one of them really isn't a log, then it wouldn't make sense to use this. But, I won't judge you if you do it anyway.



Another way to go about this would to send two files out (sendTosingle) and to write some metadata into both of them (different values of course) that allows you to route them later.



Hope that helps!

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 7:15 am
by cristalica
Hello gabrielp,



thx for your quick response, the traffic light connection is a way to do it but I would use the Errorcode to route the metadata file, I'm looking for a more elegant way.



Here a bit more to the script, it checks first if in Windows folder A is a file which has the needed name, if so the metadata file is routed to workflow folder A (otherwise sendToNull), then it checks Windows folder B for a specific file and sends it to workflow folder B if found.









I found this function in the documentation:



sendTo( c : Connection, path : String, name : String )



Sends a file/folder to the specified outgoing connection, regardless of connection type.



Is it possible to use it to send a file directly to the preferred connection or next workflow object?



The goal is to greb a file via the Inject tool from Windows folder A or B, at the moment I use 2 Inject tools but as I always have the needed file in folder A OR B, one of the tools throughs an Error and I want to get rid of this via such a pre check.

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 8:43 am
by ArielRauch
sendTo probably works in your case but I would use filter connections and set a private data key in the script. This key will serve as the decision field in the outgoing filter connections.

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 12:44 pm
by cristalica
Hi ArielRauch,



sounds it could fit to my challange, but it's also sounds complicated for my as a beginner.



Can you give me an example how to use the sendTo() function?

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 3:32 pm
by dkelly
Move connection



job.sendToSingle( job.getPath() );





Traffic light connection, 1=success, 2=warning, 3=error



job.sendToData( 1, job.getPath() ); // success data



job.sendToData( 3, job.getPath() ); // error data



job.sendToLog( 3, xmlFN, "XML" ); // error log as XML



Delete job



job.sendToNull( job.getPath() );



sendto - send file to the right connection

Posted: Mon Dec 29, 2014 4:19 pm
by cristalica
nobody using sendTo() ?



sendTo( c : Connection, path : String, name : String )



Sends a file/folder to the specified outgoing connection, regardless of connection type.

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 4:32 pm
by gabrielp
cristalica wrote: nobody using sendTo() ?
I don't use it because I don't want to edit my scripts every time I need to change a path of an output location. Instead, I'll use the other sendto functions like sendToSingle or sendToData which allows me to route jobs within the designer, keeping me out of the scripts.



As usual, I'm a little unclear with what you're trying to do here. I'm pretty sure there is another way to accomplish what you're doing without scripting (with injects and XML pickups). And even if you do end up scripting some stuff, I'd stay away from sendTo() unless you're doing to have something like a script element property where you can browse for the output location so you can specify where the file goes within Switch Designer.



If you feel like explaining what you're doing in a little more detail, with an example of your ideal lifecycle of a job, I'd be willing to help you plan this out a little more cleanly.

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 4:41 pm
by dkelly
Here's some code to dump all of the incoming and outgoing connections

var flowName = s.getFlowName();

var elementName = s.getElementName();



s.log(1, "flow name: " + flowName);

s.log(1, "element name: " + elementName);



// list in connections

var i;

var inConnectionList = s.getInConnections();

s.log(1, "In connections (" + inConnectionList.length + ")");

for (i=0; i < inConnectionList.length; i++) {

var theConnection = inConnectionList.getItem(i);

var theName = theConnection.getName();

var theType = theConnection.getConnectionType();

var theFolderName = theConnection.getFolderName();

s.log(1, (i+1) + " name: " + theName + " type: " + theType + " folderName: " + theFolderName);

}

// list out connections

var outConnectionList = s.getOutConnections();

s.log(1, "Out connections (" + outConnectionList.length + ")");

for (i=0; i < outConnectionList.length; i++) {

var theConnection = outConnectionList.getItem(i);

var theName = theConnection.getName();

var theType = theConnection.getConnectionType();

var theFolderName = theConnection.getFolderName();

s.log(1, (i+1) + "- name: " + theName + " type: " + theType + " folderName: " + theFolderName);

}

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 5:39 pm
by gabrielp
dkelly wrote: Here's some code to dump all of the incoming and outgoing connections
That's great. Thanks for sharing.

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 8:50 pm
by cristalica
The idea is simply to check folder 1 for a metadata file and if found then move on to workflow folder A. If check for folder 1 fails, then check folder 2 for another metadata file and move on to workflow folder B once found.



I want it as simply a possible for the start, something like this. SentToSingle works fine to just move on, but I cant find the right syntax to route the file to A OR B.





function jobArrived( s : Switch, job : Job )

{

var JobTicketID = job.getVariableAsString('[Metadata.Text:Path="/KONF_ID",Dataset="KonfID",Model=XML]');

var TicketPath1 = "C:/Daten1/"+JobTicketID+".xml";

var TicketPath2 = "C:/Daten2/"+JobTicketID+".xml";

if (File.exists(TicketPath1)) {

job.sendTo( A );



// s.log(1,JobTicketID);

} else {

if (File.exists(TicketPath2)) {

job.sendTo( B );







// s.log(1,"Folder " + TicketPath + " Error");

}

}

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 10:18 pm
by gabrielp
Sounds like you could use a couple XML pickups/inject, then route your failures (orphans) out of A to continue onto the path for B... Unless there's something I'm missing.



Regarding the code you posted, in the if/then. If you want to go to A, do something like job.setPrivateData("RouteTo", "FolderA") and if you want to go to B then job.setPrivateData("RouteTo", "FolderB"). Then sendToSingle.



Then, off of the outbound folder (connector -> include these jobs), route to two more folders based on private data values "FolderA" and "FolderB" for private data key "RouteTo".

sendto - send file to the right connection

Posted: Mon Dec 29, 2014 11:39 pm
by dkelly
function findConnectionByName( s : Switch, inName : String ) : Connection

{

var outConnectionList = s.getOutConnections();

for (i=0; i < outConnectionList.length; i++) {

var theConnection = outConnectionList.getItem(i);

var theName = theConnection.getName();

if (inName == theName)

return theConnection;

}

return null;

}





function jobArrived( s : Switch, job : Job )

{

var JobTicketID = job.getVariableAsString('[Metadata.Text:Path="/KONF_ID",Dataset="KonfID",Model=XML]');

var TicketPath1 = "C:/Daten1/"+JobTicketID+".xml";

var TicketPath2 = "C:/Daten2/"+JobTicketID+".xml";



var theConnectionName = "";

if (File.exists(TicketPath1)) {

theConnectionName = "A";

} else if (File.exists(TicketPath2)) {

theConnectionName = "B";

}

if (theConnectionName.length) {

var theConnection = findConnectionByName(s, theConnectionName);

if (theConnection != null) {

job.sendTo( theConnection, job.getPath() );

} else {

job.fail( "Unable to find connection named '"+theConnectionName+"'");

}

}

}


sendto - send file to the right connection

Posted: Tue Dec 30, 2014 5:50 pm
by cristalica
Hello dkelly,



great code, it works out of the box and now its more clear to me how the sendTo() function works.



@gabrilp, I'm gonna take a look at your idea tomorrow, the flexebility it is quite obvious, but its a lot new stuff to understand for me :)



Anyway, thanks everbody for all the help.



Daniel Krueger