Cookie consent

By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.

Use ServiceNow to automate the creation of a runbook

Last Updated: October 7th, 2024

Problem Statement

Cutover runbooks can be created manually using the user interface, however there are many cases where you may want to automatically create a runbook, as a result of approving a new ServiceNow change.

Solution

The functionality in ServiceNow allows you to create ServiceNow business rules that can be utilized to trigger and copy a runbook from a newly approved Change Request or a newly created incident.

Key Benefits

  • Move through Cutover at speed
  • Save time and effort
  • Less room for error
  • Ability to respond in real time

Configuring your ServiceNow Business Rules

This section provides guidance on how to create your ServiceNow Business rules. The business rule will trigger when you approve a change request and copies a new runbook from a template. This happens in real time.

Prerequisites

1. You will need to ensure you’re a ServiceNow Administrator with the ability to manage business rules and create change requests.

2. You will need Workspace Runbook Creator or Folder Runbook Creator in the appropriate workspace to create a new runbook. 

Steps for creating the ServiceNow Business Rules

In order to create the ServiceNow business rules, you will need to follow the steps below:

1. Create your business rule to trigger and copy a runbook from a template.

2. Test your business rule. 

Create your business rule to trigger and copy a runbook from a template

Once the prerequisites are met, you can create your new ServiceNow business rule by following these steps:

1. Navigate to System Definition - Business Rules >  click New.

2. You will see a page as shown in the screenshot below:

In the When to run section, update the business rule to reflect the changes shown in the screenshot above.

Note: The business rule will only trigger when a change request is updated and set to approved.

3. Click on the Advanced tab and paste in the sample code below in the script section. You will need to ensure you set the correct variables, cutover base url and bearer token.

(function executeRule(current, previous /*null when async*/ ) {
      //Set your variables
   try {
       var r = new sn_ws.RESTMessageV2();
       var crNum = current.getValue('number');
       var crSD = current.getValue('short_description');
       var crDesc = current.getValue('description');
       var crStart = current.getValue('start_date');
       var crEnd = current.getValue('end_date');
       var runbookTitle = crNum + " " + crSD;
       var runbookDesc = crDesc;
              
       //Date Processing to ISO 8601 format
       var crStartISO = crStart.substring(0,10) + 'T' + crStart.substring(11,19)+'.000Z';
       var crEndISO = crEnd.substring(0,10) + 'T' + crEnd.substring(11,19)+'.000Z';
      
       // Cutover Instance
       r.setEndpoint('https://[cutover_instance_name]/core/runbooks');
       r.setHttpMethod('POST');

       //Create Runbook API Builder
       var runbookData = {
           "data": {
               "attributes": {
                   "auto_start": true,
                   "description": crDesc,
                   "name": runbookTitle,
                   "timezone": "Europe/London",
                   "end_scheduled": crEndISO,
                   "start_scheduled": crStartISO
               },
               "type": "runbook"
           },
           "meta": {
               "shift_fixed_times": false,
               "copy": {
                   "tasks": true,
                   "teams": true,
                   "users": true,
                   "source_runbook_id": "Enter Runbook ID you wish to copy"
               }
           }
       };

       r.setRequestHeader("Authorization", "Bearer Insert Token");
       r.setRequestHeader("Content-Type", "application/json");
       r.setRequestBody(JSON.stringify(runbookData));

       var response = r.execute();
      
   } catch (ex) {
       var message = ex.getMessage();
   }
})(current, previous);

  
Copy to clipboard

4. Please ensure you save your Business Rule.

Test your business rule. 

1. Now you're ready to test your business rules. Navigate to the Change Requests page > Click New

Choose the Change Request type that you would like to raise > Fill out the Change Request form as shown above. Please ensure the Short Description, Description, Planned start date and Planned end date is set. These fields need to be populated to trigger the business rule. 

2. Once your change request has been approved the business rule will trigger and a runbook will be copied.

Note: As shown below, the runbook has been created with Short description and Change Number added to the runbook title in Cutover. Also, the runbook has kicked off at the same time as the date and time set in the Planned start date in ServiceNow. 


Available endpoints