# Workflow Example 1

##### Create workflow definition

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

[![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/ghIimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/ghIimage.png)

It will be started manually with either some provided documents for approval or it will prompt for the documents to be used for approval. The next step goes to a user responsible for the selected documents' approvals. The step after that is a 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 the "Not Approved" user task. After the corresponding last step is completed the process ends.

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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/9aximage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/9aximage.png)
    2. Click on it and from there execute "Create Subtype":  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/51mimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/51mimage.png)
    3. Name it "ApproveDocumentsProcess" with display name "Approve Documents":  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/dNuimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/dNuimage.png)
    4. In the properties add existing properties Assignee, Due Date and Description:  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/Dvtimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/Dvtimage.png)
    5. Click the "Lens" button next to the Properties field and click "Create New" -&gt; "Property":  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/v66image.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/v66image.png)
    6. Fill the following details (name: documents, count: 1 or more, property type: Document):  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/T0Nimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/T0Nimage.png)
    7. Edit property placements to your likeness:  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/CHXimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/CHXimage.png)
        
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/PXJimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/PXJimage.png)
    8. Click the "Create" button.
2. Similarly create types for the two approval tasks: 
    1. You will need two new properties - Comments and Approved:  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/KJRimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/KJRimage.png)
        
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/rggimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/rggimage.png)
    2. The Workflow Approve Task:  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/GONimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/GONimage.png)
    3. Workflow Approve Done Task:  
        [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/YJnimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/YJnimage.png)

Now we are ready to create the workflow definition:

1. From the Workflows dropdown menu select "Create New":  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/qBHimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/qBHimage.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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/A5Aimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/A5Aimage.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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/82bimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/82bimage.png)
4. Create two Execution listeners for **start** and **complete** and put the corresponding inline Groovy scripts to each of those:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/LXlimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/LXlimage.png)
    
    The script is: ```java
    def workflowInitiatorUser = helpers.createCamundaObjectWrapper(userHelpers.getUserById(workflowInitiatorSubject, godUser), godUser)
    
    execution.setVariable('workflowInitiatorUser', workflowInitiatorUser)
    ```
    
    It sets the variable **workflowInitiatorUser** which will 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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/q77image.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/q77image.png)
6. Click "Change Element" and select "User Task":  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/9Uoimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/9Uoimage.png)
    
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/1vcimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/1vcimage.png)
7. Set the properties of the step:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/oO2image.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/oO2image.png)
    
    For **Assignee** enter: `${assignee != null ? assignee.id.toString() : null}` . For **Due Date** enter: `${dueDate == null ? null : dueDate.toDate()}`.  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/yocimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/yocimage.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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/Z45image.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/Z45image.png)
    
    Script is:  
    ```java
    def addedProperties = [
      description: description,
      documents: documents.collect { it.id },
    ]
    
    helpers.updateObject('task:' + task.id + '@6', godUser, addedProperties)
    ```
    
    which sets the **description** and **documents** properties to the task.
    
      
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/wmyimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/wmyimage.png)  
    Script for the **complete** event is:
    
    ```java
    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 the **approved** and **comment** properties to the process.
9. Add and connect the next steps, and fill their properties:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/qzBimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/qzBimage.png)
10. Select the "No" path and set it as "Default flow":  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/LWWimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/LWWimage.png)
11. Select "Yes" flow and set its properties:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/l0Qimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/l0Qimage.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. Similarly to the **Approve** task set properties of the "**Approved**" and "**Not Approved**" tasks:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/9VMimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/9VMimage.png)
    
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/4yIimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/4yIimage.png)
    
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/6Tximage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/6Tximage.png)
    
    For inline script use:  
    ```java
    def addedProperties = [
      comment: comment,
      documents: documents.collect { it.id },
      description: description,
      approved: approved,
    ]
    
    helpers.updateObject('task:' + task.id + '@6', godUser, addedProperties)
    ```
    
    which sets the corresponding properties from the process to tasks.
13. Enter the name of the workflow:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/tZCimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/tZCimage.png)
14. Click the "**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](https://profuzlapis.com/download/Approve%20Documents.bpmn). The types and properties created for the above example can be downloaded from [here](https://profuzlapis.com/download/Approve%20Documents%20WF%20types%20export.json) and then can be imported through **Operations -&gt; Import/Update from JSON**.

##### Test the new workflow

1. From the **Approve Documents** workflow definition's object menu select "Start Workflow":  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/PdOimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/PdOimage.png)
2. The form from the "ApproveDocumentsProcess" type will be shown to enter appropriate properties:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/usMimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/usMimage.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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/4iyimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/4iyimage.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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/Jklimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/Jklimage.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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/UEximage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/UEximage.png)
2. Enter the following in **Type Object Header Zone**:  
    [![image.png](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/LB8image.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/LB8image.png)
    
      
    The code is:
    
    ```vbscript
    {{#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](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/scaled-1680-/foKimage.png)](https://docs.profuzdigital.com/uploads/images/gallery/2025-01/foKimage.png)