Server

HOWTO: Write Reusable Automation Chains

Updated: December 4, 2024

You might have some automation chains in your project that all have a similar structure, meaning the same operations in the same order, the only difference being that the parameter values sent to the composing operations are different. Usually in this case, you can factorize your implementation using parameterized chains.

Let's take an example: You want to change the lifecycle of a document, while logging the company of the user who changed the document lifecycle state in the audit. 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 lifecycle 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 lifecycle transitions.