Events

Version 22.0.8483


Events

Version 22.0.8483


There is often a need to integrate external information into data flow tasks. Sync offers Pre-Job and Post-Job events to enable such scenarios. These events allow you to inject query parameters or trigger other external processes by utilizing APIScript, an XML-based language included with CData Sync.

Complete code snippets for automating common actions, such as running a batch file, sending an email or triggering a new Job execution are included with CData Sync. On the Events tab for your job, click Insert Snippet and select the action you want to perform. The necessary code is inserted at the cursor.

Pre-Job Event

The Pre-Job event fires before the job begins executing queries. The specifics about the job including the name of the job, the source, the destination etc. are available as input parameters and can be used to make logical choices within the event script. You can also push attribute values in the output item that can later be used during the execution of the Job.

<api:info title="Before Run" desc="This event is fired before running a job.">
  <input name="JobName"      required="true" desc="The name of the job being executed." />
  <input name="Source"       required="true" desc="The name of source connection." />
  <input name="Destination"  required="true" desc="The name of destination connection." />
  <input name="JobStatus"    required="true" desc="The previous job status." />
  <output name="env:*"                       desc="A variable to be used later in the Job" />
  <output name="CancelJob"                   desc="Set to true if you want cancel job execution." />
</api:info>

Creating Environment Variables

Environment Variables can be created in the Pre-Job event script and added to the Job execution. The following example creates a currentTimeStamp variable with the current timestamp and pushes the out item with that value to the Job:

<api:set attr="out.env:currenttimestamp"  value="[null | now()]" />
<api:push item="out" />

Environment Variables pushed to the job can be used in many different ways in the Replicate Query. To reference an environment variable, surround the env.attribute with curly braces {}. The below example sets the value of currenttimestamp to the new column DateUpdated. Note,

REPLICATE [Table] SELECT [ID], [Name], '{env:currenttimestamp}' AS [DateUpdated] FROM [Table]

Post-Job Event

The Post-Job event fires after the job finishes executing queries. The input item includes results of the Job execution that can be used for calling other operations.

<api:info title="After Run" desc="This event is fired after running a job.">
  <input name="JobName"         required="true" desc="The name of the job being executed." />
  <input name="Source"          required="true" desc="The name of source connection." />
  <input name="Destination"     required="true" desc="The name of destination connection." />
  <input name="JobStatus"       required="true" desc="The status of the completed run."/>
  <input name="Query#"          required="true" desc="An array containing each query that was executed." />
  <input name="QueryStatus#"    required="true" desc="An array containing the status of each query that was executed." />
</api:info>

Triggering a Job Execution

Other Jobs can be triggered in the Post-Job event. The below example will execute NextJob after the current Job finshes running queries. You can also extend this example by using the api:if keyword to only execute NextJob if the current Job succeeds.

<!-- Start Executing different Job -->
<api:set attr="job.JobName"        value="NextJob"/> 
<api:set attr="job.ExecutionType"  value="Run"/> 
<api:set attr="job.ASync"          value="false"/>
<api:call op="syncExecuteJob" in="job"/>

Extending the Example Scripts

The included snippets only scratch the surface of what is possible. The API Script language can handle complex business logic and comes with a lot of built-in operations that make things like sending an email, or moving a file, etc. easy. You can also pass in additional inputs that are specific to the operation. For example, the appSendEmail operation accepts several additional inputs, such as the body text, attachments, and SSL/TLS properties. You can provide these values with the api:set keyword. For Example

With the api:call keyword, you can call any of the built-in operations in an event and also make calls to the API.

Use other APIScript keywords to control execution flow and more. APIScript is computationally complete. It also has many enhancements designed to make processing and automation easy. See Intoduction to APIScript for more information on the syntax of APIScript and its capabilities.