# ServiceNow

### **Setup the App administrator account**

**App admin url**: <https://developer.servicenow.com/dev.do#!/home>

Once the app admin account is created and verified you need to create an instance.

<figure><img src="https://content.gitbook.com/content/cfhstxIZQWZNvWjV2O6F/blobs/Cp8AKtSvadeVfbGC2w7O/servicenow-1.png" alt=""><figcaption></figcaption></figure>

It will take a minute ~~or two~~ ~~or three~~ or five to complete.

<figure><img src="https://content.gitbook.com/content/cfhstxIZQWZNvWjV2O6F/blobs/MobbbuqAKrBeDiFnNDue/servicenow-2.png" alt=""><figcaption></figcaption></figure>

Now your developer instance is ready and accessible: <https://dev180380.service-now.com/>. Note that when creating a new account, your instance ID (`dev180380` in this case) will be different.

### Setup Webhooks

To configure webhooks in ServiceNow to receive notifications when a new record is created, follow these steps. This involves creating an Outbound REST Message, setting up a Business Rule to capture record creation, and configuring the webhook.

#### Create an Outbound REST Message:

* Navigate to **System Web Services > Outbound > REST Message**.
* Click on the **New** button to create a new REST Message.
* Fill in the fields such as:
  * Name: e.g., `AppmixerNotifications`.
  * Endpoint: `https://<YOUR_API_BASE>/plugins/appmixer/servicenow/events` for example `https://api.appmixer.com/plugins/appmixer/servicenow/events` ![Outbound REST](https://github.com/clientIO/appmixer-docs-gitbook/blob/v6.0/app-registration/assets/servicenow_2.png)
* Click **Submit** and then open the newly created REST Message.
* Click on the **HTTP Methods** tab and create a new HTTP Method.
* Fill in the details such as:
  * Name: e.g., `events`.
  * HTTP Method: POST.
  * Endpoint: Ensure this is filled with your webhook listener’s URL.
* Optionally, add default HTTP Request Headers (e.g., Content-Type) and customize the Request Body or Query Parameters if needed.
* Save the HTTP Method.

#### Create a Business Rule:

* Navigate to the table for which you want to capture new record creation. For example, if it’s the Incident table, go to **Incident > All**.
* Right-click on the form header and select **Configure > Business Rules**.
* Alternatively, you can navigate to **Activity Subscriptions -> Administration -> Business Rules**.
* Click on the **New** button to create a new Business Rule.

  * Set the fields as follows:
  * Name: e.g., `Incident Rules`.
  * Table: Select the appropriate table (e.g., `Incident`).
  * When: Insert.

  ![Business Rule](https://content.gitbook.com/content/cfhstxIZQWZNvWjV2O6F/blobs/j0mNCG7zXXjzP57MNEU6/servicenow_1.png)
* Under **Advanced**, check the box **Advanced** to open the script editor.
* In the Script section, add the following code:

  * Note that `AppmixerNotifications` should be the same as the name of the Outbound REST Message in the previous step.

  ```js
  function serializeGlideRecordToJSON(gr) {
      const obj = {};
      const fields = gr.getFields();

      for (let i = 0; i < fields.size(); i++) {
          const field = fields.get(i);
          const fieldName = field.getName();

          // skipping sys_id and other sensitive fields if necessary
          if (fieldName !== 'sys_id' && fieldName !== 'sys_updated_on' && fieldName !== 'sys_created_on') {
              obj[fieldName] = gr.getValue(fieldName);
          }
      }

      return obj;
  }

  if (current.operation() === 'insert') {

      const rq = new sn_ws.RESTMessageV2('AppmixerNotifications', 'events');
      const requestBody = {
          'type': 'incident.insert',
          'data': serializeGlideRecordToJSON(current)
      };

      rq.setRequestBody(JSON.stringify(requestBody));
      rq.execute();
  }

  ```

  The code above handles only new records in the `incidents` table. If you want to track new items in other tables, create another business rules and send the notifications to the Appmixer endpoint, where the `type` of the event is in format like: '.insert':

  ```js
  const rq = new sn_ws.RESTMessageV2('AppmixerNotifications', 'events');
  const requestBody = {
      'type': 'tasks.insert',
      'data': serializeGlideRecordToJSON(current)
  };

  rq.setRequestBody(JSON.stringify(requestBody));
  rq.execute();
  ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.appmixer.com/6.0/6.1/app-registration/servicenow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
