Skip to main content

Workflow Example 1

Create workflow definition

Let create a simple workflow for document approval. Its process will look line this:

image.png


It will be started manually with either provided documents for approval or it will prompt for the documents to be used for approval. The next stem goes to a user responsible for the selected documents approvals. The next step is decision gateway - it checks if the documents are approved or not. If they are approved the flow goes to the Approved user task step, which are assigned to the approval requested user. In not, it goes to "Not Approved" user task. After the corresponding last step is completed the process end.

Follow these steps to create this workflow:

  1. We will need a custom workflow instance type and two types of workflow tasks: Workflow Approve Task and Workflow Approval Done Task.
    1. For this go to Administration/Types and search for workflow instance:
      image.png
    2. Click on it and from there execute "Create Subtype":

      image.png


    3. Name it "ApproveDocumentProcess" with display name "Approve Documents":

      image.png


    4. In the properties add existing properties Assignee, Due Date and Description:

      image.png


    5. Click the "Lens " button next to the Properties field and click "Create New" -> "Property":

      image.png


    6. Fill the following details (name: documents, count 1 or more, property type: Document):

      image.png


    7. Edit property placements to your likeness:

      image.png

      image.png


    8. Click "Create" button.
  2. Similarly create types for the two approval tasks:
    1. You will need ne properties Comments and Approved:

      image.png

      image.png


    2. The Workflow Approve Task:

      image.png


    3. Workflow Approve Done Task:

      image.png


Now we are ready to create the workflow definition:

  1. From the Workflow select "Create New":

    image.png


  2. Set the properties of the process. Expand "General", fill Name and ID (must be unique in your installation) and check "Executable":

    image.png


  3. Select the start event (the only circle in the process diagram) and fill its properties. For the Form key put the name of newly created Workflow Instance's subtype "ApproveDocumentsProcess":

    image.png


  4. Create two Execution listeners for start and complete and put the corresponding inline groovy scripts to each of those:

    image.png

    The script is:
    def workflowInitiatorUser = helpers.createCamundaObjectWrapper(userHelpers.getUserById(workflowInitiatorSubject, godUser), godUser)
    
    execution.setVariable('workflowInitiatorUser', workflowInitiatorUser)

    it sets variable workflowInitiator to be used in other parts of the workflow.


  5. Click on "Append Task" next to the Start event to create the next step:

    image.png


  6. Click "Change Element" and select "User Task":

    image.png

    image.png


  7. Set the properties of the step:

    image.png

    For Assignee enter: ${assignee != null ? assignee.id.toString() : null } . For Due Date enter: ${dueDate == null ? null : dueDate.toDate()}.
    image.png
    For Form Key use the name of the newly created workflow task type WorkflowApproveTask.

  8. Add two Task listeners for create and complete events:

    image.png

    Script is:
    def addedProperties = [
      description: description,
      documents: documents.collect { it.id },
    ]
    
    helpers.updateObject('task:' + task.id + '@6', godUser, addedProperties)

    which sets description and documents properties to the task.


    image.png
    Script for complete event is:

    def thisTask = helpers.getObjectById('task:' + task.id + '@6', godUser)
    
    task.execution.setVariable('approved', thisTask.get('approved'))
    task.execution.setVariableLocal('comment', thisTask.get('comment'))

    which sets approved and comment properties to the process.


  9. Add and connect the next steps, and fill their properties:

    image.png


  10. Select the "No" path and set it as "Default FLow":

    image.png


  11. Select "Yes" flow and set its properties:

    image.png

    Condition expression is: ${approved != null && approved } , i.e. this path is taken only when the approved is set to true in the Approve task.
  12. Similar to Approve task set properties of the "Approved" and "Not Approved" tasks:

    image.png

    image.png

    image.png

    For inline script use:
    def addedProperties = [
      comment: comment,
      documents: documents.collect { it.id },
      description: description,
      approved: approved,
    ]
    
    helpers.updateObject('task:' + task.id + '@6', godUser, addedProperties)

    which sets corresponding properties from the process to tasks.


  13. Enter the name of the workflow:

    image.png


  14. Click "Create" button and your new workflow definition is ready and can be used to create instances based on it. 

You can download the BPMN of the above example from here.

Test the new workflow
  1. From the Approve Documents workflow definition's object menu select "Start Workflow":

    image.png

  2.  

    The form from the "ApproveDocumentsProcess" type will be shown to enter appropriate properties:

    image.png

  3.  

    The newly created instance of the workflow will be shown where you can observe the active steps and the history of the process:

    image.png

  4.  

    In history you can click an active step to go to it and observe its status (if you have rights of course):

    image.png

      
Add a button in the header of Document objects to start approval process with the above workflow
  1. Go to Document type:

    image.png

  2.  

    Enter the following in Type Object Header Zone:

    image.png


    The code is:

    {{#let (service "session") as |session|}}
      {{#let-unless-falsy arg=(get (live-search query='[:types INHERITS "WorkflowDefinition"] AND [:bpmnName "Document Approval Workflow"] SORT BY :name ASC LIMIT 1') 0) checkForEmptiness=true as |newApproveDocument|}}
        <OperationButton @operationName="StartWorkflowOperation" @object={{newApproveDocument}} @parameters={{hash documents=(array @model.id)}} @buttonClass="btn btn-warning">
          Request Approval
        </OperationButton>
      {{/let-unless-falsy}} 
    {{/let}}
  3. On your document's header the following button will be shown from which you directly can start the approval workflow:

    image.png