Code Task

Using Code Tasks for Complex Logic in Liberate

In Liberate, you have the option to add a code task to your workflows when you need to implement more complex logic. Currently, Liberate supports Python and JavaScript as the programming languages for code tasks. This feature allows developers to write custom code within their workflows to perform specific actions or transformations that may not be easily achieved with built-in features or tasks.

Why Use Code Tasks:

There are several scenarios where using a code task can be beneficial:

  • Custom Logic: Code tasks provide the flexibility to implement custom logic tailored to your specific requirements. If the standard set of tasks and features in Liberate doesn't fully address your automation needs, you can use code tasks to write custom functions, algorithms, or data transformations.
  • Integration with External Systems: Sometimes, you may need to interact with external systems or APIs that require specific authentication mechanisms or have complex data processing requirements. With code tasks, you can integrate external libraries or write custom code to handle these interactions seamlessly within your workflows.
  • Data Manipulation: Code tasks allow you to perform complex data manipulations or transformations that may not be easily achieved through mapping tasks or other built-in features. By leveraging the power of programming languages like Python or JavaScript, you can manipulate and process data in a highly flexible and efficient manner.
  • Conditional Logic and Flow Control: Code tasks enable you to implement conditional logic, loops, and flow control statements within your workflows. This gives you more control over the execution path and allows you to handle complex branching scenarios or iterative processes.

Example: Python Code Task

Here's an example of a Python code block used as a code task in a Liberate workflow. Let's explain what's happening in this code block

def handler(context):
    ocr_data = context['invoiceOCRData']
    tables = ocr_data['all_tables']
    activities_table = []

    for table in tables:
        if not table:
            continue
        if len(table[0]) != 9:
            continue  # Not an activities table - must have 9 columns
        if table[0][0] == 'Date' and table[0][1] == 'Adj.':
            pass
        else:
            continue
        activities_keys = []
        for col_name in table[0]:
            activities_keys.append(col_name)
        for row in table[1:]:
            activity = {}
            for col_index in range(len(row)):
                activity[activities_keys[col_index]] = row[col_index]
            activities_table.append(activity)
    return {
        "activities": activities_table
    }

In this code block, the handler function takes a context argument, which represents the context data passed to the code task. The code performs the following actions:

  1. It accesses the 'invoiceOCRData' key from the context, assuming it contains OCR data related to invoices.
  2. It retrieves all the tables from the OCR data.
  3. It initializes an empty activities_table list.
  4. It iterates through each table and applies certain conditions to identify an "activities table" that should have nine columns.
  5. If the table meets the conditions, it extracts the column names and iterates through the rows to create dictionaries representing activities.
  6. Finally, it appends each activity dictionary to the activities_table list.
  7. The code task returns the resulting activities_table as the output, encapsulated in a dictionary with the key "activities".

This code block demonstrates how you can use a code task to process and transform data within your workflow, based on specific conditions and requirements.