# Create Business Rules

## Introduction

The ServiceNow Table API allows programmatic interaction with incidents, providing functionalities for creating, searching, updating, and deleting incidents.

To securely use this API, various authentication methods are available. In this guide, we will focus on using the API key for authentication.

In this guide, we will learn to:

- Set up a Business Rule to automatically send a request to a webhook whenever a record is created.

## Prerequisites

Before getting started, make sure you have:

- A ServiceNow administrator account
- The URL of your ServiceNow instance

## Walkthrough

To configure a trigger that sends a request whenever a specific condition is met, navigate to **`All > Activity Subscriptions > Administration > Business Rules`** and create a new rule.

   <div class="docs-screenshot-compact">

   ![Url your servicenow instance](./assets/url-your-servicenow-instance.png)

   </div>

## Configuring the Business Rule

In the rule configuration, fill in the following fields:

1. **Table :** Select the target table (e.g., **Incident [Incident]**).
2. **Advanced :** Check the **Advanced** option to display the **Advanced** tab, where you can add a custom script.
3. **When to run**
   - **When after**: Choose this option to specify that the rule should run after the event has occurred.
     - If you experience issues to send outbound requests with “Illegal access to Outbound HTTP”, turn **When** to **async**.
   - **Select insert**: Select **Insert** (equivalent to creating a new record).
   - **Order**: Leave this option unchanged.
   - **Filter**: Do not modify this option.
4. **Advanced**
   - **Script**: In this tab, add the custom script that will perform the desired action (e.g., send a request to a webhook or perform other specific actions). In the payload, you can include the fields that the request should contain. Find below a script example.

     ```jsx
     ;(function executeRule(current, previous /*null when async*/) {
       var restMessage = new sn_ws.RESTMessageV2()
       restMessage.setEndpoint("https://webhook-{your-env}.mindflow.io/playbooks/{playbook-id}/steps/start/run-async")
       restMessage.setHttpMethod("post")
       restMessage.setRequestHeader("Content-Type", "application/json")
       restMessage.setRequestHeader("x-api-key", "your Mindflow api key")
       var payload = {
         request_id: current.sys_id.toString(),
         request_number: current.number.toString(),
         created_date: current.sys_created_on.toString(),
         updated_date: current.sys_updated_on.toString(),
       }
       restMessage.setRequestBody(JSON.stringify(payload))
       try {
         var response = restMessage.execute()
         var httpResponse = response.getStatusCode()
         gs.info("Webhook Response: " + httpResponse)
       } catch (error) {
         gs.error("Error sending webhook: " + error.message)
       }
     })(current, previous)
     ```

   <div class="docs-screenshot-compact">

   ![Script](./assets/script.png)

   </div>

     On the side, you can select the fields to include in the payload.

## Retrieving Webhook URL and API Key in Mindflow

To obtain the asynchronous webhook URL to paste in the script below and the API key for the **Header**, follow these steps in Mindflow:

1. **Add the Webhook Trigger**

- At the beginning of your **playbook**, add a **Webhook trigger** to initiate the workflow.

   <div class="docs-screenshot-compact">

   ![Beginning your playbook add webhook trigger](./assets/beginning-your-playbook-add-webhook-trigger.png)

   </div>

1. Open the **Settings** tab to view the webhook details.

   <div class="docs-screenshot-compact">

   ![Open settings tab view webhook details](./assets/open-settings-tab-view-webhook-details.png)

   </div>

- **Copy the asynchronous webhook URL**: This URL will be used to receive triggered events.
- **Get the API key**: In the **Header** section, locate the key **x-api-key** and copy its value. This key is required to authenticate requests sent to the webhook.
