# How to automatically log incidents in ServiceNow using Leapwork?

**Introduction**  
This article explains how Leapwork can be integrated with ServiceNow to create incidents when automation flow fails.

**Preconditions**  
Following prerequisites are required for integration.

1. HostName or IP Address and API port of Leapwork Controller.  
2. The [API Access key](https://support.leapwork.com/s/article/HowtoaddAPIAccessKeystoaLeapworkController63307917ec203) for a given Leapwork Controller.  
3. Organize and execute flow in a [Run List](/content/product/documentation/run-lists/index.html).  
4. The run-list is scheduled using the Leapwork [scheduler](/content/product/documentation/schedules/index.html) and the schedule name is noted.  
5. Service now credentials with API calling access.  
6. Basic understanding of PowerShell or coding.

**Create Incidents in ServiceNow**  
The Leapwork public REST API makes it very easy for DevOps, IT operations, and developers to integrate Leapwork with any third-party system. This article details how to automatically create incidents in ServiceNow when automation flows in Leapwork fails.

As a part of the integration, we have a Windows PowerShell script that runs a pre-defined Leapwork run list, polls for the results until they are available, and then loops through all failed cases and creates incidents in ServiceNow using API.

1. **Download the attached Integration [PowerShell script](https://leapworkaps.file.force.com/sfc/dist/version/download/?oid=00D7Q000003Qjj6&ids=0687Q000004sArT&d=%2Fa%2F7Q000000cB1L%2FPCbRIUCypt_lyAS8y3lkkObNjXYrh8iNSyGHRJTzfNI&asPdf=false)** and open it in your preferable editor. We are going to use the Windows PowerShell ISE editor to update the script.

2. The first two things you need to change in PowerShell Script is the Controller hostname and the controller port. The script has localhost as the hostname and default port 9001 as a port. In order to point the script to call your controller endpoints, the user has to update the values of these two global variables viz. **$controllerHostNameOrIPAddress** and **$controllerPort**.
    ```powershell
    $controllerHostNameOrIPAddress="localhost"
    $controllerPort="9001"
    ```

3. Set service now credentials and the URL of your instance to the PowerShell script. The below variables need to be supplied value.
    ```powershell
    $serviceNowUser = "username"
    $serviceNowPass = "password"
    $serviceNowInstance = "https://yourdomain.service-now.com"
    $incidentOwnerEmail = "callie.leboeuf@example.com"
    ```

4. Create an API access key in Leapwork Controller and note it down.
5. Open Powershell script and replace the generated key with the Access key value in the header section as shown below.
    ```powershell
    $APIAccesskey="12345"
    ```

6. Verify that the flow you want to run are there in the Run list and the Runlist has a Schedule associated with it (refer: Preconditions, point number 3 and 4), Make a note of the Schedule name and replace the Schedule name in Powershell with the name of schedule you have.
    ```powershell
    $scheduleName="SmokeRunSchedule"
    ```

7. Run the Powershell script. When you run the PowerShell script, the schedule will be executed and for failed flows, incidents will be logged in ServiceNow as described below.
   
   - The value of the variable will be assigned to global variables.
   - RunScheduleAndGetResults Method will be called which will call the Leapwork API endpoint to execute the schedule and polls for the result until that is available.
   
    ```powershell
    # Run the Leapwork schedule "My Test Schedule" and get the results.
    $runResult = RunScheduleAndGetResults($scheduleName)
    ```

- Once the result is available, the script will try to find the number of the failed cases. If the number of failed tests is greater than zero then it will start collecting logs and screenshots.
   
   It will call the service now endpoint recursively i.e., in a loop for logging the incidents.

Service now endpoint will file incidents on service now until all of them are logged.

Here is how an incident logged by the above script looks like in service now.

Please note that the script contains no error handling or logging mechanisms. It is meant only to demonstrate the core functionality of integrating Leapwork with ServiceNow.

For any clarification, contact our [Priority Support](mailto:prioritysupport@leapwork.com).

**Attachments**
- [LEAPWORK_-SNOW_integration.ps1](https://leapworkaps.file.force.com/sfc/dist/version/download/?oid=00D7Q000003Qjj6&ids=0687Q000004sArT&d=%2Fa%2F7Q000000cB1L%2FPCbRIUCypt_lyAS8y3lkkObNjXYrh8iNSyGHRJTzfNI&asPdf=false)
- [LEAPWORK_-SNOW_integration.txt](https://leapworkaps.file.force.com/sfc/dist/version/download/?oid=00D7Q000003Qjj6&ids=0687Q000004sAh7&d=%2Fa%2F7Q000000cB1Q%2FKr8C._G7RxF9QugFpSyI4OJd5tJU78eSV5NmD9QB4j0&asPdf=false)
