Custom Code

The Custom Code integration task evolves the DRUID Connector into a business operations layer. Use it to implement business rules that always apply either to the flow engine, web views, forms, and external applications.

To define the business rules, you write custom JavaScript code.

Use cases

  • Perform data validations and raise errors when the data validation fails. For example:

    • When updating a client account check that it includes valid person ID, first name and last name, regardless of where the save is triggered: flow engine, web form or external applications.
    • When creating a new account, check that there is no other account in external system with the same personal identification code regardless of where the save is triggered: flow engine, web form or external applications.
  • Do extensive calculations. E.g., perform forecast calculations.
  • Transform the data
  • Execute conditional tasks.
  • Move business logic from the flows into integrations.

Prerequisite

  • You should have medium to extensive JavaScript knowledge.

How to use the Custom Code integration task

To add a Custom Code task in the Connector Diagram, click the Palette icon, the click on Integration - Common. Drag Custom Code onto the diagram and drop it where you want the integration to be executed.

In the Description field, provide a brief, relevant description of the business rule to be implemented.

The Code Extension field contains a default code snippet for the Custom Code task.

To write your own code, we recommend maximizing the code snippet editor for better visibility.

You can either modify the default snippet or use a blank template and function templates, which are categorized under Add snippet. To learn more about available functions and templates, refer to the relevant section in this topic.

Use JavaScript to implement your business rule. The Request Entity and Response Entity are displayed at the top of the Code Extension field for reference.

Once your code is complete, save the code snippet.

Save the integration.

The section below provides the different template snippets and function templates you can use to write your business rules in the code snippet.

Templates

Context

Operational

Entity

Note:  You can refer to entities and entity collections by name in the Custom Code editor. If you use the Entity explorer when creating an entity or entity collection, remove the brackets “[[ ]]”.

Variables

Local scope

Encryption

XML

FileUtils

Testing the custom code integration task and code tracing

You can test the execution of your code and manually verify in the Code Trace that the code works correctly or if there are any logic errors.

Code tracing is an effective tool in debugging efforts at authoring time as it traces the execution of all the lines in the code and enables you to validate your code.

To test the code, click on the Test tab, then click the Test button () at the top-right corner of the page.

The Code Trace traces the execution of your Custom Code integration task and enables you to:

  • View the points where the execution has failed (highlighted in red). If the code execution returns errors, you can debug the code by going through the CodeTrace and see what went wrong.
  • View the lines of code which were not executed (grayed lines) and the lines that were successfully executed (marked with a green line on the left).
  • View what changed in an entity by clicking the compare icon first for the reference entity data and then for the updated entity data. A page opens displaying side-by-side the initial (reference) and the updated entity data.
  • View an entity as JSON by clicking on the magnifier icon.
  • View the audit log for Context.ExecuteTask and see the values returned from the executed task if it was executed successfully, or the error if the task execution failed.