Sample Two: Launch .BAT File

This sample script launches a .bat file during a transition. It can be used in the pre-transition, post-transition, pre-state, or post-state contexts.
Note: To use this script, you must add it to SBM using the Scripts editor in SBM Composer. You must then associate it with a transition or state in the workflow in which the state or transition was defined. Make sure that you remove any arrows that are used to indicate line breaks first.

Launch .BAT File Script Contents

' SBM ModScript Example: Launch.tscm

/* ModScript Example: Launch.tscm
  ------------------------------
  This script runs a .BAT file, passing the values of
  selected fields.  The script waits for the .BAT file
  to finish before exiting.  By replacing "NewTaskWait"
  with "NewTask", the script can be made to exit immediately,
  leaving the .BAT running in the background.
 
  Requirements:
  This script relies on Shell.Item(), which only exists in
  pre-transition, post-transition, pre-state, and post-state
  contexts.
 
  Note: if reading a drop-down list field, do not use the
  GetFieldValue() function.  Instead, retrieve the actual
  Field object, and use its GetDisplayValue() method.
*/

// MODIFY THIS CONSTANT FOR YOUR DATABASE
// --------------------------------------
// Name of the field we will work with
add_global_const( "Title", "FLDNAME" ); 
// add a global constant called "FLDNAME" with the value "Title". 
The keyword "global" could also be used, but then the value is not constant

// MODIFY THIS CONSTANT FOR YOUR SYSTEM
// ------------------------------------
// Path to the .BAT file
add_global_const( "C:\\ModScript\\run.bat", "PATH" ); 
// add a global constant called "PATH" with the value "C:\ModScript\run.bat" 
(notice backslashes were escaped by backslash)

// Gather params from item fields, pass them to .BAT file.
def LaunchBatchFile() {
  var issueID;
  var fldValue = Variant();
  var exitCode;
  
  // Construct display ID from item type and item ID
  issueID = Shell.Item().GetDisplayIssueId();

  // Read the field
  if ( !Shell.Item().GetFieldValue( FLDNAME, fldValue ) ) {
    // Error finding the field or reading from it
    Ext.LogErrorMsg( "Cannot read from field \"" &&& FLDNAME &&& "\"" );
    return;
  }

  // Run the .BAT file with params
  exitCode = Ext.NewTaskWait( PATH, issueID, "\"" &&& fldValue &&& "\"" );

  /* NOTE: When params are passed to a .BAT file, they are concatenated into
     a command-line-style string separated by spaces.  Inside the .BAT file,
     the params are parsed using spaces and quotes.  Thus, if fldValue
     contains quotes, there can be problems receiving the data.
     To work around this problem, call a .EXE file instead of a .BAT file. */

  if ( exitCode < 0 ) {
    // Negative exit code means OS-level error, such as .BAT file not found.
    Ext.LogErrorMsg( "Error launching \"" &&& PATH &&& "\" with params:\r\n  " &&&
                     issueID &&& "\r\n  " &&& fldValue &&& "\r\n" );
  }
}


// Find the item being transitioned
if ( Ext.ShellHasProp( "Item" ) ) {
  LaunchBatchFile();
} else {
  // There is no current item, so write a message to the event viewer
  Ext.LogErrorMsg( "ModScript error: Shell.Item() does not exist." );
}

.BAT File Script Contents

The following contents must be added to the .bat file called by the Launch .BAT File script example.

@ECHO off
REM --- run.bat
REM --- This is an example batch program to be called
REM --- from the SBM ModScript example "Launch.tsc"
REM --- It appends a new line to C:\out.txt each time it runs
ECHO Launched run.bat, params (if any): %* >> C:\out.txt