Handle response

Overview

The handleResponse method is an optional Groovy method that can be defined in a task or an executable. It is called automatically after an API request is completed and allows you to process the API response before the task continues.

The method receives a single ServiceAPIResult parameter, which contains the response returned by the API, including the response data and status information.

You can use handleResponse to:

  • Process or validate the API response.
  • Extract values from the response.
  • Reuse the response data when calling additional APIs, without querying the database again.
  • Perform custom logic based on the API response, such as deciding what to do next.

Groovy code

import com.ria.converter.api.executor.service.ServiceWorkEntry
//import com.riaadvisory.dih.core.api.executor.service.ServiceWorkEntry

def handleResponse(ServiceAPIResult result) {
    // Log the invocation for debugging purposes
    CommonEnv.logInfo('Invoking target T1_MOVIE for ' + result.getServiceWorkEntry().getColumnValue('MOVIE_TITLE'))

    // Extract column and field values from the ServiceAPIResult
    def colValues = new HashMap(result.getServiceWorkEntry().getColumnValues())
    def fieldValues = new HashMap(result.getServiceWorkEntry().getFieldValues())

    // Print column and field values for debugging purposes
    println "colValues = ${colValues}"
    println "fieldValues = ${fieldValues}"

    // Invoke another target API using the extracted values
    CommonEnv.invokeTarget('T1_DIRECTOR', 1, colValues, fieldValues)
}
Parameter Description
ServiceAPIResult result Contains the result from the previous API call. The ServiceAPIResult object provides access to details such as column values and field values.
getColumnValues()

Returns the source column values as a HashMap. You can reuse these values when calling another target that uses the same source, avoiding another database query.

If the target uses different columns, you must manually populate the required values in the Groovy script.

getFieldValues()

Returns the mapped field values as a HashMap. You can reuse or modify these values before invoking another target.

If a field is not provided, the target's mapping expression is used. The mapping expression can be a literal value or a column name from the Column Values map, but it cannot be an SQL function because that would require another database query.

CommonEnv.invokeTarget()

Invokes another target from the Groovy script using the supplied target ID, mapping ID, column values, and field values. This uses the following parameters:

  • Target Id – Identifier of the target to invoke.
  • Mapping Id – Unique identifier of the target mapping to execute. This is the internal mapping identifier, not the mapping sequence.
  • Column Values HashMap – Map of source column names and values passed to the target. It reuses getColumnValues() if the target uses the same source, or create the map manually if it does not.
  • Field Values HashMap – Map of field names and values passed to the target. Fields not included in the map are populated using the target's mapping expressions.

Examples

Executable

Task

Restartability

This solution does not currently support restarting from a specific API call made in the handleResponse method.

If a failure occurs, DIH restarts from the task’s primary target, not from the API call that failed. As a result, the primary target may be called again even though it completed successfully before the failure, which can lead to duplicate data being sent to the target system.

To avoid duplicate API calls, design the source query for the primary target so that it excludes records that have already been processed successfully.