I have a vbscript that creates automatically a incident in ServiceNow. Tested and it works.
But I wish to make it more practical and funcional. I am looking for a form based on this vbscript in order to create the incident.
Can you guys help me compiling a form based on the vbscript I am posting bellow? Many thanks.
Option Explicit
Const gServiceNowUser = "username"
Const gServiceNowPass = "password"
Const gServiceNowURL = "Class ServiceNowDirectWS
' Use this class to call ServiceNow Direct Web Services functions
' For documentation on the Direct WS API see:
'
Dim sEndpointURL, sTableName, sMethod, sResponsePath
Dim oWSRequest, oWSRequestDoc, oWSResponseDoc
Dim oWSRequestEnvelope, oWSRequestBody, oWSRequestOperation
Public Sub SetMethod (tableName, method)
' This function must be called BEFORE Post to initialize the class
' method must be "insert", "update", "getKeys", "get" or "getRecords"
sTableName = tableName
sMethod = method
sResponsePath = "/soap:Envelope/soap:Body/" & sMethod & "Response/"
sEndpointURL = gServiceNowURL & sTableName & ".do?SOAP"
If (sMethod = "get" Or sMethod = "getRecords") Then
sEndpointURL = sEndpointURL & "&displayvalue=all"
End If
Set oWSRequest = CreateObject("MSXML2.XMLHTTP")
Set oWSRequestDoc = CreateObject("MSXML2.DOMDocument")
Set oWSRequestEnvelope = oWSRequestDoc.createElement("soap:Envelope")
oWSRequestEnvelope.setAttribute "xmlns:soap", _
" Set oWSRequestBody = oWSRequestDoc.createElement("soap:Body")
Set oWSRequestOperation = oWSRequestDoc.createElement("tns:" & sMethod)
oWSRequestOperation.setAttribute "xmlns:tns", _
" & sTableName
oWSRequestDoc.appendChild oWSRequestEnvelope
oWSRequestEnvelope.appendChild oWSRequestBody
oWSRequestBody.appendChild oWSRequestOperation
End Sub
Public Function Post
' This function does the actual Web Services call
' It returns True if the call is successful and False if there is an error
oWSRequest.open "POST", sEndpointURL, False, gServiceNowUser, gServiceNowPass
oWSRequest.setRequestHeader "Content-Type", "text/xml"
oWSRequest.send oWSRequestDoc.xml
If oWSRequest.status = 200 Then
Set oWSResponseDoc = CreateObject("MSXML2.DOMDocument")
oWSResponseDoc.loadXML oWSRequest.responseText
oWSResponseDoc.setProperty "SelectionLanguage", "XPath"
oWSResponseDoc.setProperty "SelectionNamespaces", _
"xmlns:soap=' Post = True
Else
Set oWSResponseDoc = Nothing
Post = False
End if
End Function
Public Function Status
' If Post returns False then call this function to obtain the HTTP status code
Status = oWSRequest.status
End Function
Public Function StatusText
' If Post returns False then call this function for the error text
StatusText = oWSRequest.statusText
End Function
Public Sub SetValue(fieldname, fieldvalue)
' This function must be called BEFORE Post
Dim oChild
Set oChild = oWSRequestDoc.createElement(fieldname)
oChild.appendChild(oWSRequestDoc.createTextNode(fieldvalue))
oWSRequestOperation.appendChild(oChild)
End Sub
Public Function GetValue(fieldname)
' This function must be called AFTER Post
' If method is "insert" then it can be used to obtain the sys_id of the inserted record
' If method is "get" then it can be used to obtain any field from the record
GetValue = oWSResponseDoc.selectSingleNode(sResponsePath & fieldname).text
End Function
Public Function GetRowCount
' This function may be called after Post if the method is "getRecords"
' It returns the number of records in the result set
Dim sResultsPath, oNodeset
sResultsPath = sResponsePath & "getRecordsResult"
Set oNodeSet = oWSResponseDoc.selectNodes(sResultsPath)
getRowCount = oNodeSet.length
End Function
Public Function GetRowValue(rownum, fieldname)
' This function may be called after Post if the method is "getRecords"
' It returns a single field from a single record
Dim sRowPath, sFieldPath
sRowPath = sResponsePath & "getRecordsResult[" & rownum & "]/"
sFieldPath = sRowPath & fieldname
GetRowValue = oWSResponseDoc.selectSingleNode(sFieldPath).text
End Function
End Class
' Specify the ticket values
Dim wsInsertIncident : Set wsInsertIncident = New ServiceNowDirectWS
wsInsertIncident.SetMethod "incident", "insert"
wsInsertIncident.SetValue "short_description", "Incident Test"
wsInsertIncident.SetValue "description", "Incident Test"
wsInsertIncident.SetValue "caller_id", "My Username"
wsInsertIncident.SetValue "u_category", "Category Name"
wsInsertIncident.SetValue "u_subcategory", "Sub Category Name"
wsInsertIncident.SetValue "u_masiva", "Massive"
' Perform the insert and check the status
If Not wsInsertIncident.Post Then
WScript.Echo "Error=" & wsInsertIncident.Status
WScript.Echo wsInsertIncident.StatusText
WScript.Quit
End If
Dim strIncidentSysId, strIncidentNumber
strIncidentSysId = wsInsertIncident.GetValue("sys_id")
strIncidentNumber = wsInsertIncident.GetValue("number")
WScript.Echo "Inserted: " & strIncidentNumber
But I wish to make it more practical and funcional. I am looking for a form based on this vbscript in order to create the incident.
Can you guys help me compiling a form based on the vbscript I am posting bellow? Many thanks.
Option Explicit
Const gServiceNowUser = "username"
Const gServiceNowPass = "password"
Const gServiceNowURL = "Class ServiceNowDirectWS
' Use this class to call ServiceNow Direct Web Services functions
' For documentation on the Direct WS API see:
'
Dim sEndpointURL, sTableName, sMethod, sResponsePath
Dim oWSRequest, oWSRequestDoc, oWSResponseDoc
Dim oWSRequestEnvelope, oWSRequestBody, oWSRequestOperation
Public Sub SetMethod (tableName, method)
' This function must be called BEFORE Post to initialize the class
' method must be "insert", "update", "getKeys", "get" or "getRecords"
sTableName = tableName
sMethod = method
sResponsePath = "/soap:Envelope/soap:Body/" & sMethod & "Response/"
sEndpointURL = gServiceNowURL & sTableName & ".do?SOAP"
If (sMethod = "get" Or sMethod = "getRecords") Then
sEndpointURL = sEndpointURL & "&displayvalue=all"
End If
Set oWSRequest = CreateObject("MSXML2.XMLHTTP")
Set oWSRequestDoc = CreateObject("MSXML2.DOMDocument")
Set oWSRequestEnvelope = oWSRequestDoc.createElement("soap:Envelope")
oWSRequestEnvelope.setAttribute "xmlns:soap", _
" Set oWSRequestBody = oWSRequestDoc.createElement("soap:Body")
Set oWSRequestOperation = oWSRequestDoc.createElement("tns:" & sMethod)
oWSRequestOperation.setAttribute "xmlns:tns", _
" & sTableName
oWSRequestDoc.appendChild oWSRequestEnvelope
oWSRequestEnvelope.appendChild oWSRequestBody
oWSRequestBody.appendChild oWSRequestOperation
End Sub
Public Function Post
' This function does the actual Web Services call
' It returns True if the call is successful and False if there is an error
oWSRequest.open "POST", sEndpointURL, False, gServiceNowUser, gServiceNowPass
oWSRequest.setRequestHeader "Content-Type", "text/xml"
oWSRequest.send oWSRequestDoc.xml
If oWSRequest.status = 200 Then
Set oWSResponseDoc = CreateObject("MSXML2.DOMDocument")
oWSResponseDoc.loadXML oWSRequest.responseText
oWSResponseDoc.setProperty "SelectionLanguage", "XPath"
oWSResponseDoc.setProperty "SelectionNamespaces", _
"xmlns:soap=' Post = True
Else
Set oWSResponseDoc = Nothing
Post = False
End if
End Function
Public Function Status
' If Post returns False then call this function to obtain the HTTP status code
Status = oWSRequest.status
End Function
Public Function StatusText
' If Post returns False then call this function for the error text
StatusText = oWSRequest.statusText
End Function
Public Sub SetValue(fieldname, fieldvalue)
' This function must be called BEFORE Post
Dim oChild
Set oChild = oWSRequestDoc.createElement(fieldname)
oChild.appendChild(oWSRequestDoc.createTextNode(fieldvalue))
oWSRequestOperation.appendChild(oChild)
End Sub
Public Function GetValue(fieldname)
' This function must be called AFTER Post
' If method is "insert" then it can be used to obtain the sys_id of the inserted record
' If method is "get" then it can be used to obtain any field from the record
GetValue = oWSResponseDoc.selectSingleNode(sResponsePath & fieldname).text
End Function
Public Function GetRowCount
' This function may be called after Post if the method is "getRecords"
' It returns the number of records in the result set
Dim sResultsPath, oNodeset
sResultsPath = sResponsePath & "getRecordsResult"
Set oNodeSet = oWSResponseDoc.selectNodes(sResultsPath)
getRowCount = oNodeSet.length
End Function
Public Function GetRowValue(rownum, fieldname)
' This function may be called after Post if the method is "getRecords"
' It returns a single field from a single record
Dim sRowPath, sFieldPath
sRowPath = sResponsePath & "getRecordsResult[" & rownum & "]/"
sFieldPath = sRowPath & fieldname
GetRowValue = oWSResponseDoc.selectSingleNode(sFieldPath).text
End Function
End Class
' Specify the ticket values
Dim wsInsertIncident : Set wsInsertIncident = New ServiceNowDirectWS
wsInsertIncident.SetMethod "incident", "insert"
wsInsertIncident.SetValue "short_description", "Incident Test"
wsInsertIncident.SetValue "description", "Incident Test"
wsInsertIncident.SetValue "caller_id", "My Username"
wsInsertIncident.SetValue "u_category", "Category Name"
wsInsertIncident.SetValue "u_subcategory", "Sub Category Name"
wsInsertIncident.SetValue "u_masiva", "Massive"
' Perform the insert and check the status
If Not wsInsertIncident.Post Then
WScript.Echo "Error=" & wsInsertIncident.Status
WScript.Echo wsInsertIncident.StatusText
WScript.Quit
End If
Dim strIncidentSysId, strIncidentNumber
strIncidentSysId = wsInsertIncident.GetValue("sys_id")
strIncidentNumber = wsInsertIncident.GetValue("number")
WScript.Echo "Inserted: " & strIncidentNumber