Automation

How to Write Reusable Automation Chains

Updated: October 16, 2020

In your project, you might have some automation chains that all look the same, with the same structure, meaning the same operations in the same order, and the only difference is that the parameters values sent to the composing operations are not the same. This usually means you can factorize your implementation using parameterized chains.

Let's take an example: You want to change the life cycle of a document, and log at the same time the company of the user who changed the document life cycle state in the audit. Genuinely, if you want a chain that validates a document, you would write the following chain:

- Context.FetchDocument
- Document.SetLifeCycle:
    value: approve
- Audit.Log:
    event: test
    category: Automation
    comment: "@{CurrentUser.company}"

If you want to do it for each life cycle changes (making it obsolete, or draft, ...), you would have to write several chains calling the Document.SetLifeCycle and Audit.Log operations.

Or your can use a parameterized chain:

  1. Create a chain called "ChangeCaseStatus".

    params:
    - transitionName:
        type: string
    - userPropertyToLog:
        type: string
    operations:
    - Context.FetchDocument
    - Document.SetLifeCycle:
        value: "@{ChainParameters['transitionName']}"
    - Audit.Log:
        event: test
        category: Automation
        comment: "@{CurrentUser.getProperty(ChainParameters['userPropertyToLog'])}"
    

    To declare the parameters of a chain, you can use the Chain Parameters tab of the Automation Chain feature in Nuxeo Studio. To reference a parameter's value inside the chain:

    @{ChainParameters['parameterName']}

  2. Create a second chain that references the first chain with a Run Chain operation, using the parameters field. This second chain is used to approve document.

    - Context.RunOperation:
        id: ChangeCaseStatus
        isolate: 'false'
        parameters:
          userPropertyToLog: company
          transitionName: approve
    

    You can then leverage this chain with a user action.

  3. Repeat step 2 to create as many chains as you need to follow the different life cycle transitions.