On occasions you may want to call a task from within another and execute it to run.
This may be if a particular process spawns multiple tasks, a modular approach is being used to scale down complexity, or to relieve file lock scenarios.
Triggering another task is a simple case of using a VBScript step that runs the TaskCentre API. Sample code is shown below which demonstrates how a task can be called to run from another, it also shows how variable or recordset data can be passed from the first task to the second.
Prerequisites #
Create a TaskCentre user with sufficient privileges to run any tasks. Restrict this user for API operations to prevent user account locking.
START OF CODE
Dim Server, UserName, Password Dim TCAPI, Session, TaskItem, TaskID Dim Variable1 Const LogonTaskcentre = 1 Const LogonWindows = 2 'Provide the ID of the task you wish to run TaskID = 'xxxxx 'Supply the location of the TaskCentre Server and the credentials of a TaskCentre user that will run the task Server = "localhost" UserName = "API_User" Password = "password" 'Connect to the TaskCentre API and login Set TCAPI = CreateObject("Iwcltcp.TCAPI") Set Session = TCAPI.Logon(LogonTaskcentre, Server, UserName, Password) Set TaskItem = Session.OpenTaskItem(TaskID) 'This enables variables to be passed to the task being called to run Set Vars = TaskItem.Variables 'Assign a value to a declared variable Variable1 = 'This may be a variable or recordset value from the task browser 'Specify a variable in the task being called to run and declare a value to be passed to it at run time Vars.Item("CustomerID").ParameterValue = Variable1 'Run the second task, the first task will wait for it to complete 'This enables variable data from the second task to be passed back to the first task TaskItem.Runsync Variable1 = Vars.Item("CustomerID").ParameterValue 'Or 'Run the second task, the first task will also run without waiting for the second task 'This does not allow variable data to be passed back 'TaskItem.QueueTask
END OF CODE
In its most basic form, if you simply wish to trigger a task to run when one completes, then the following VBScript may be used:
START OF CODE
Dim Server, UserName, Password Dim TCAPI, Session, TaskItem, TaskID Const LogonTaskcentre = 1 Const LogonWindows = 2 Server = "localhost" 'server address required UserName = "username" 'user required Password = "password" 'password required TaskID = 'task id required Set TCAPI = CreateObject("Iwcltcp.TCAPI") Set Session = TCAPI.Logon(LogonTaskcentre, Server, UserName, Password) Set TaskItem = Session.OpenTaskItem(TaskID) TaskItem.QueueTask
END OF CODE
Before using the VBScript please be aware of the following points:
- You must create or use a valid user in order to access the API. In the above example we have used a user called ‘API_User’ with a password of ‘password’.
- You must ensure the variables you wish to populate in the task you are calling have been created. In the above example we are loading a variable called ‘CustomerID’ which has been created in the second task.
- You must be aware of whether you require the tasks to run in or out of sync with each other as this will effect the outcome of the process.
- When you’re passing data from a recordset to a variable in the task you are calling, you MUST ‘Dim’ a variable within the script and load that with the recordset value, then pass that into the variable in the new task. For example:
Dim Variable1
Variable1 = ThisStep.RecordSource(“DemoColumn”)
Vars.Item(“TaskVariable1”).ParameterValue = Variable1