Blog

ServiceNow to Microsoft Excel Integration

This is a good example of how to use REST Webservices with ServiceNow.

Here is the video from youtube that details how to do this.  The video shows the major points and it pretty cool. However it misses some critical steps which I detail below (Step 3 in particular)

 

How to Connect ServiceNow to Excel using REST Webservices

Step 1: Google Chrome Advanced REST Client Extension

1. Watch the video above.
2. Read the Wiki on REST Webservices
3. Download and install the Chrome Advanced Rest Client Extension
4. Use the Chrome REST Plugin to put in REST details
- GET (That pulls data from ServiceNow)
- Instance: https://<yourinstance>.service-now.com/api/now/table/incident
For <yourinstance>, try a developer instance of ServiceNow.  Don't try on your company's instance first.
5. Add Headers (in the form section)
Accept | application/xml
Authorization | Click Contruct in value field
Enter Basic login (Note the user account you use needs the admin, rest_webservices, and/or itil role)
6.  Run the query.  If successful, you will need to copy the details to notepad (especially the authorization string, which is something like Basic Yxxxxxxx32434Mw==)  You will need that in the excel part.

Step 2: Create the Excel File

1. Create a new excel file
2. Create two new worksheets: incidents, problem.  (Note the spelling is important)
2. Enable the "Developer" tab in the Ribbon if not enabled.
3. Click Developer > Visual Basic
4. Click New > Module
5. Paste this code in there from the video (switch out the InstanceURL and Authorization code lines with your info)

Sub ServiceNowRestAPIQuery()

' Replace with your Service Now Inctance URL
InstanceURL = "https://dev16551.service-now.com"
' Replace with your Authorization code
AuthorizationCode = "Basic YWRtaW46U0NONHF1aWw="
' Add more tables ascomma seperated with no spaces
TableNames = ("incident,problem")

Dim ws As Worksheet
Dim objHTTP As New WinHttp.WinHttpRequest
Dim columns As String
Dim Header As Boolean
Dim jsonString As String
Dim Resp As New DOMDocument
Dim Result As IXMLDOMNode
Dim ColumnsArray As Variant

TablesArray = Split(TableNames, ",")

For x = 0 To UBound(TablesArray)

'Table Choices
Select Case TablesArray(x)

 Case "incident"
Set ws = Sheets("incidents")
columns = "number,company,close_notes,impact,closed_at,assignment_group"
ColumnsArray = Split(columns, ",")
OtherSysParam = "&sysparm_limit=100000"
SysQuery = "&sysparm_query=active%3Dtrue"
 
Case "problem"
'Sheet name
Set ws = Sheets("problem")
'Columns to Query
columns = "number,short_description,state"
ColumnsArray = Split(columns, ",")
'Query filter Parameters
OtherSysParam = "&sysparm_query=state=1"
'Other Query Parameters
SysQuery = ""
 End Select

URL = InstanceURL & "/api/now/table/"
Table = TablesArray(x) & "?"
sysParam = "sysparm_display_value=true&sysparm_exclude_reference_link=true" & OtherSysParam & SysQuery & "&sysparm_fields=" & columns
URL = URL & Table & sysParam
objHTTP.Open "get", URL, False
objHTTP.SetRequestHeader "Accept", "application/xml"
objHTTP.SetRequestHeader "Content-Type", "application/xml"

' Authorization Code
objHTTP.SetRequestHeader "Authorization", AuthorizationCode
objHTTP.Send '("{" & Chr(34) & "short_description" & Chr(34) & ":" & Chr(34) & "Test API2" & Chr(34) & "}")

Debug.Print objHTTP.Status
Debug.Print objHTTP.ResponseText
ws.Select
Header = False
i = 1
Range("A1").Select
Cells.Clear

Resp.LoadXML objHTTP.ResponseText
For Each Result In Resp.getElementsByTagName("result")
 For n = 0 To UBound(ColumnsArray)
If Header = False Then
ActiveCell.Offset(0, n).Value = ColumnsArray(n)
End If
ActiveCell.Offset(i, n).Value = Result.SelectSingleNode(ColumnsArray(n)).Text
Next n
i = i + 1
Header = True
Next Result
 'MsgBox Time
Next x
End Sub

Step 3: Setup Visual Basic

1. Click the Play button in Visual Basic.  Notice that nothing works
2. Click View > Immediate Window
3. Click the Play button again in Visual Basic.  Notice the error messages in the immediate window.  It is missing important libraries to run.
4. Click Tools > References.  Check these libraries
- Microsoft WinHttp Services, version 5.1
- Microsoft xml v6.0
5. Click Ok
6. Run again.  Notice data in now in your excel sheets.