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.

Launch .BAT File Script Contents

' SBM AppScript Example: Launch.tsc

' ------------------------------
' 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.
' Require all variables to be declared before use
Option Explicit
' MODIFY THIS CONSTANT FOR YOUR DATABASE
' --------------------------------------
' Name of the field we will work with
const FLDNAME = "Title"
' MODIFY THIS CONSTANT FOR YOUR SYSTEM
' ------------------------------------
' Path to the .BAT file
const PATH = "C:\AppScript\run.bat"
' Find the item being transitioned
If Ext.ShellHasProp( "Item" ) Then
  LaunchBatchFile
Else
  ' There is no current item, so write a message to the event viewer
  Call Ext.LogErrorMsg( "SBM AppScript error: Shell.Item does not exist." )
End If
' Gather params from item fields, pass them to .BAT file.
Sub LaunchBatchFile()
  Dim issueID, fldValue, exitCode, QUOTE
  QUOTE = Chr( 34 )  ' the only way to get a quote in VBScript
  ' Construct display ID from item type and item ID
  issueID = Shell.Item.GetDisplayIssueId()
  ' Read the field
  If Not Shell.Item.GetFieldValue( FLDNAME, fldValue ) Then
    ' Error finding the field or reading from it
    Call Ext.LogErrorMsg( "Cannot read from field " & QUOTE & FLDNAME & QUOTE )
    Exit Sub
  End If
  ' Run the .BAT file with params
  exitCode = Ext.NewTaskWait( PATH, issueID, QUOTE & fldValue & QUOTE )

  ' 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 Then
    ' Negative exit code means OS-level error, such as .BAT file not found.
    Call Ext.LogErrorMsg( "Error launching " & QUOTE & PATH & QUOTE _
                      & " with params:" & vbCrLf _
                      & "  " & issueID & vbCrLf _
                      & "  " & fldValue & vbCrLf )
  End If
End Sub

.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 AppScript 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