# Adding a Conditional Step in a Digital Process

Having the first **Hello World** digital process set up, we can now add conditional logic to the way it works. The condition would include sending an additional email notification, but only if a sender provides us with a specific response to a form. To use conditional logic and add a condition step with a decision rule in the Hello World digital process, we will first prepare a new Make scenario and then create a business rule in OE Rulestore.

## Creating a new scenario to respond to sender needing outreach

Similar to the **Setting up a First Digital Process**, we will create a scenario named **Send Outreach on Mixed Feedback** that sends another email to forms recipients.

To build the scenario:

1. Go to **OE** -> **Events** -> **Event Registry** and create a new `hello_outreach_sent` event named **Hello World Outreach Sent**.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-4f84418f2da8bda1e3c01084904ccb3dd18d1749%2Ftrail2_outreach_event.png?alt=media" alt=""><figcaption></figcaption></figure>
2. Go to **Digital Processes** and open the **Hello World** process that you created in the first trail.
3. Add a new process step and choose to **Create New Scenario**. This directs you to a Make scenario template.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-e9fa53f3fa6fbe52a3b7cd01cd96af6aee71b759%2Ftrail2_newstep.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>
4. As a first step of the Make scenario, establish the connection in the **Trigger Event** module.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-2c242f0930f660b5837dd431111d750cef6c4e01%2Ftrail2_webhook.png?alt=media" alt="" width="485"><figcaption></figcaption></figure>
5. Add the **Set Variable from Process Context** module and configure it with the following details:

   * Assign the Data from the **Trigger Event**.
   * Select the **Hello World** digital process.
   * Use the Instance ID from as the **Example Instance**. You can select the ID from the drop-down list. The ID at the top is the one from the latest instance.
   * Choose **Hello World Feedback Sent** as an **Event Type**.
   * Choose `email` from the **Field** drop-down list.
   * Type `email` in the **Variable Name** field.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-7e384f7f80f5a0617e0f73732ebe6137041e8f6d%2Ftrail2_set_variable.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>
6. Add the second **Set Variable from Process Context** module and configure it with the following details:

   * Assign the Data from the **Trigger Event**.
   * Select the **Hello World** digital process.
   * Use the latest Instance ID as the **Example Instance**. You can select the ID from the drop-down list. The ID at the top is the one from the latest instance.
   * Choose **Hello World Feedback Provided** as an **Event Type**.
   * Choose `forms.0.payload.firstName` in the **Field** drop-down list.
   * Type `firstName` in the **Variable Name** field.
   * Choose **Yes** for **Raise Error when Null**.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-0e2e44d884f89bacf29a9136ef23805f557e3e3d%2Ftrail2_set_variable2.png?alt=media" alt="" width="487"><figcaption></figcaption></figure>
7. Add the **Mailgun** (Send an Email) module. We use Mailgun as the exemple email provider as in the **Setting up a First Digital Process** trail. Ensure you allowlist the email recipient in your Mailgun account, so that the email gets sent. To make the module work, you need to set it up as described in [Make's Mailgun module](https://www.make.com/en/help/app/mailgun) documentation. Use the following Mailgun module configuration:

   * Choose a specific email address as the **Type of recipient**.
   * Enter the email address of the recipient in the **To** -> **Add items** list.
   * Add the sender email address.
   * Add the subject of the message and insert the text that you want to send.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-9479ad4dfb6652bf148db3fa9ebca5baa2163522%2Ftrail2_mailgun.png?alt=media" alt="" width="402"><figcaption></figcaption></figure>
8. In the **Completion Event**, choose the **Hello World Outreach Sent** event as the **Event Type**.
9. **Save** the scenario and choose **Run once**.
10. Go to **OE** and open the **Hello World** digital process. Add a new process step with the new scenario, you can see that the **Send Outreach on Mixed Feedback** scenario is now visible in the available scenarios list. Choose the scenario as the process step.

    <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-bf77660e60c97f99f62bb7c2d238ec2d4fc5c170%2Ftrail2_outreach_step.png?alt=media" alt="" width="562"><figcaption></figcaption></figure>
11. Choose **Publish** and then ensure that the digital process is active.
12. Ensure that the **Send a Survey on Hello World** and **Send Outreach on Mixed Feedback** scenarios in Make are active as well.
13. To test, re-run the new instance of the digital process by sending a new trigger event. You can send the same event using the Postman request, or Celonis action flows, that you used in the **Setting up a First Digital Process** trail to start the digital process.

After sending the event, first you get an email requesting you to submit a response to the created form. And then, after submitting the response, you get a second email with a Thank You message.

## Applying conditional logic with the use of a business rule

Up to this point the **Send Outreach on Mixed Feedback** scenario was invoked by any response submitted by the form recipient. Now, we will limit this process to invoke the scenario only on `indifferent` or `sad` responses. To do that, we first need to create a business rule and then apply it in the **Hello World** digital process as a decision on a process step.

### Creating a business rule

Emporix Orchestration Engine provides a **Rulestore** that allows to manage business rules outside of the digital processes and Make scenarios. The rules can be expressed with JsonLogic. Using JsonLogic, we will create a rule that identifies sentiment responses that need further outreach.

To create a new rule:

1. In **Management Dashboard**, go to **OE** -> **Rulestore** and choose **Create rule**.
2. Choose if you want to create the rule in **Simple** or **Advanced** mode.

   * If you choose the **Simple** mode, add the following values in the blocks:
     * Name: `Sentiment Requiring Outreach`
     * Description: for example - `measure how positive recipients are`
     * Unit: `list`
     * Expression: `in`
     * Value: `indifferent`, `sad`

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-7307f01cd10d94e6cf499ecfdd170e46b06995d4%2Ftrail2_newrule_simple.png?alt=media" alt=""><figcaption></figcaption></figure>

   * If you choose the **Advanced** mode, add the following:
     * Name: **Sentiment Requiring Outreach**
     * Rule Expression: add a JsonLogic expression that evaluates to `true` if a variable called `sentiment` has `indifferent` or `sad` values. The editor validates the expression syntax, so it's possible to save only the valid forms:

   ```json
   {
       "in": [
           {
               "var": "sentiment_requiring_outreach"
           },
           [
               "indifferent",
               "sad"
           ]
       ]
   }
   ```

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-e59855ce49f90a36619ed6a092059ca226baaed3%2Ftrail2_newrule.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>
3. Save the rule. It's now visible in the **Rulestore** list.
4. Copy the rule's ID, it will be used in the next steps.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-8345dd07a206dfbd4dbb7f0fc6db7f9c19b81a91%2Ftrail2_rulelist.png?alt=media" alt="" width="563"><figcaption></figcaption></figure>

### Applying the rule to the digital process as a condition for a process step

When we have the business rule created in a Rulestore, we can now use it as a condition for a process step in the **Hello World** digital process.

To apply the business rule:

1. Go to the **Hello World** digital process and in the **Send Outreach on Mixed Feedback** process step choose **Add logic**.
2. Go to the **Decision** tab. Here, you can see the **JSON Logic Expression** field, where you can insert the business rule that you had prepared for the process step.

   <figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-a4a405c85e43956f07cb520b19afe3a4a4fbecbd%2Ftrail2_condition_step.png?alt=media" alt="" width="551"><figcaption></figcaption></figure>

   Enter the following expression, with your business rule ID:

```json
{
    "businessRule": [
        "01HPH99SX29GB5Y6GXXNBW6GF1",
        [
            "sentiment_requiring_outreach",
            {
                "context": "hello_feedback_provided.forms.0.payload.sentiment"
            }
        ]
    ]
}
```

The `businessRule` is a JsonLogic operator that refers back to the rule managed in the **Rulestore**.

* The first part is the rule ID that you have to copy from the **Rulestore** and then paste in the expression.
* The second part maps the rule variables to the actual relevant fields in your process context. Here, we are saying that the `sentiment_requiring_outreach` field can be found in the `hello\_feedback\_provided.forms.0.payload.sentiment` context event field.

3. Choose **OK** to save the expression as the process step decision.
4. Publish the new version of the digital process and ensure it's active. Now, only the responses with **indifferent** or **sad** values result in running the **Send Outreach on Mixed Feedback**.

   However, the digital process completes regardless of the response that is submitted in the form.
5. Send the event request again by Postman or Celonis action flows. As a result, you receive a submission form request again.
   * In the first form submission provide the answer as **happy**. This will not execute the **Send Outreach on Mixed Feedback** process step.
   * If you provide the response as **indifferent** or **sad** the **Send Outreach on Mixed Feedback** is invoked and you receive another email with the Thank You form and information about the next steps that you included in the text.

You can also use the debugger mode to verify if the digital process works properly. Go the **Hello World** digital process and choose the magnifier icon to open the debug mode.

<figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-1c72c0568c76215ea88fe0e441ece426a7dfe619%2Ftrail2_debug.png?alt=media" alt=""><figcaption></figcaption></figure>

* If the response is **happy**, you can see that the step was filtered.

<figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-d187aacc083899f250463a41082bfd3e4473af14%2Ftrail2_debug_happy.png?alt=media" alt="" width="545"><figcaption></figcaption></figure>

* If the response was **indifferent** or **sad**, the step was invoked.

<figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-aa0f6a3aad73e1b65202dcc70f83f9f9544bd99e%2Ftrail2_debug_unhappy.png?alt=media" alt="" width="545"><figcaption></figcaption></figure>

It is also possible to use events themselves as conditions on process steps. Multiple events can be assigned to a step or group of steps, and both `IN (OR)` and `AND` operators are supported. It's a preferred option when it is simply enough to test the presence of an event.

<figure><img src="https://1593395737-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQHHnOQFqJkmze1kPDN3S%2Fuploads%2Fgit-blob-773d530a72791ce16b9d0ff8df42fcd6a2af3079%2Ftrail2_filters.png?alt=media" alt="" width="552"><figcaption></figcaption></figure>

{% hint style="info" %}

* To learn more about the conditional logic in OE, see the [**Conditional process runs**](https://developer.emporix.io/oe/oe-celonis/digital-processes/working-with-digital-processes/conditional-process-runs) documentation.
* To learn more about JsonLogic and get some examples, see the [Supported Operations](https://jsonlogic.com/operations.html) and [Play with JsonLogic](https://jsonlogic.com/play.html) websites.
  {% endhint %}
