ServiceNow REST API to create Incident Ticket using C#

Share this
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

In this article you will learn to use ServiceNow REST API to create Incident Ticket using C#.

Complete source code can be found at GitHub repo :

https://github.com/sudipta-chaudhari/ServiceNow_CreateIncident_CSharp

Many of the leading IT organizations use some Incident Management tools and our project’s support team use ServiceNow as their incident management tool for incident management.

From the suite of products, a popular product is for Incident Management. You can read more about this product at URL : https://www.servicenow.com/products/it-service-automation-applications/incident-management.html

ServiceNow is also referred as SNOW. I will refer to ServiceNow as SNOW in the coming parts of this post.

Objective/Use Case: Create incident ticket in SNOW whenever an application error occurs. The application can be any Web App, Windows App, Windows Service or any other type of app. Whenever any error or any specific type of error occurs in the application, an incident ticket should be created in SNOW by the application.

Solution: To implement a solution for the above use case, we need to find out how we can integrate SNOW in our application using a programming language like C#. You can go to SNOW portal and manually create an incident ticket. But that’s not what is needed. The incident ticket should be created in SNOW as soon as any error or a specific kind of exception occurs in out application.

SNOW exposes REST API’s using which it can be integrated with our application. Please see SNOW Wiki reference for REST API at http://wiki.servicenow.com/index.php?title=REST_API#gsc.tab=0

REST API endpoint for creating incidents is :

where instance is your organization’s registered instance. This is a POST API where you need to authenticate using Basic Authentication and pass in the post body a number of fields or data which are like incident’s short description, description, assignment group, priority (P1, P2, P3 etc), category, sub-category, comments etc.

Follow the below steps to create a C# console application for generating SNOW incident:-

(1) Create C# Console App
(2) Add app.config file keys by creatin node
(3) Create folder logger and add new class Logger
(4) Add references : “System.Configuration”. Add Nuget package : Netweonsoft.JSON
(5) Add using “System.Net”, “System.IO”, “Newtonsoft.JSON”, “Newtonsoft.JOSN.LINQ”
(5) Add code for SNOW tikcet creation

I will describe the C# method that will call this SNOW REST API endpoint which can be used to create a SNOW incident. I have created a class named Logger in which I have created the below method.

In this method, we construct a ‘Basic Authentication’ object, then make a POST request to the SNOW Incident creation REST Service endpoint. In the POST request body, the below parameters are inserted with their corresponding values:

description, shortDescription, category, subcategory, assignment_group, impact, priority, caller_id , cmdb_ci, comments

The POST call sends the JOSN response back which indicates success or failure. The success JSON response contains the incident ticket number which can be extracted from the response using a JSON parser like Newtonsoft.JSON and querying JSON with SelectToken.

CreateIncidentServiceNow() method finally returns the SNOW Incident number for the created incident.

Various SNOW parameters are stored in the application’s configuration file used in the above function are as below.

In the catch block of you try/catch error handler’s, you can call this method as :

The above catch block calls the ‘CreateIncidentInServiceNow’ method in which the error message and error stack trace is passed. The error message will be the SNOW incident’s short description and the error stack trace will be the SNOW incident’s description.

The incident created in SNOW looks as shown in below screenshot.

SNOW_Exception

Hope you followed the article. If you have any comments, questions or suggestions, leave a message and I will try to respond at my earliest.


Share this
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

19 thoughts on “ServiceNow REST API to create Incident Ticket using C#”

  1. Hi..Thanks for the article…
    I have one question, will the SNOW url be same for all company exception the company name in the beginning.
    what are those credentials..if you have access to Service now will those credentials work or they are specifically for calling the service and we need to get them from the client.
    Please confirm.

    Thanks again…

    1. Hi Bilal,

      Instance name is configured by ServiceNow admin who configures it for the specific organization. So, the URL looks like : https://xyzorg.service-now.com/

      The credentials (username and password) will also be configured by ServiceNow admin. If you have the credentials, those credentials will work for calling the rest api endpoint for creating incident but that depends on what role has been granted to you by ServiceNow admin of your organization.

  2. Hi Sudipta. I am a contractor working on a PC Security project. We Just switch over to ServiceNow. Before we was using CA Manager and I had a VB Script and a C# program for creating bulk tickets. I now programming but I am not well versed by any means. I can take snippet of code, understand it and modify it to meet my needs. I see you have a program in C# for creating a ticket in ServiceNow. I am not familiar with ServiceNow and its REST API. Would I need to work with ServiceNow technical support to get what you wrote to work?

    1. Dear Colin,

      Previous to using ServiceNow (SNOW), my organization was using CA Service Desk (CASD) as the primary incident/request/change management tool. Later it was changed to ServiceNow.

      Every tool like CASD or SNOW provides web services/REST services to interact with their product programatically (using code).

      It depends on what you need to achieve in ServiceNow like – retrieve existing incidents, create an incident record, read incident, update incident, delete incident etc. Please visit the URL : ServiceNow REST Api documentation for complete ServiceNow REST api documentation.

      If you contact ServiceNow support, you have to provide exact details about your query else your may either not be answered or would be redirected to the documentation page. If you or your organization are registered with ServiceNow, you will get the reply faster. Non registered users may or may not receive a reply from ServiceNow support.

      In my article, I have called ServiceNow REST api to create incident using C#. ServiceNow provided Web Services also to achieve the same functionality like creating tickets and much more. However, Web Service is an older technology and REST Service is the latest technology.

      Please feel free to comment if you have further queries.

  3. I need to create new incidents.
    I can create incidents through the web interface. My Windows logon credentials is authenticated. Would I be given a separate username and password in order to use the REST Api to create the in incident?

    1. Dear Colin,

      In my case, I logon to the ServiceNow web inerface https://.service-now.com and authenticate using Office 365 OAuth organizational authentication.

      For creating incidents using ServiceNow REST api, I have a different set of credentials configured by my organization’s ServiceNow admin.
      In your case as you authenticate to the ServiceNow web interface using Windows Authentication, you might be needed separate credentials to call ServiceNow REST api to create incident tickets.

      I would advise you to contact your ServiceNow administrator or you can also contact ServiceNow support who would help in authenticating against ServiceNow REST api’s.

      Let me know how it goes and please feel free to post if you have further queries.

      Thanks and regards,
      Sudipta

    1. Hi Arup,

      For creating a service request in ServiceNow using REST API, choose the ‘Category’ as ‘Request’ and ‘Sub Category’ as set by the ServiceNow admin which can be as ‘Complaint’, ‘Request for Change’, ‘Support Request’ etc.

      In the article’s code, I have created web.config keys – “ServiceNowCategory” and “ServiceNowSubCategory”. Change these key’s values to ‘Request’ and ‘Service Request’.

      You can also yourself see these options if you go to your organization’s Service Now URL and select ‘Ticket’ -> ‘Create New’ and select ‘Category’ as ‘Request’.

      Please feel free to comment if you have further queries.

      Thanks and regards,
      Sudipta

  4. Hi, Can any please post an example for incident ticket creation/updation/cosure in service now using SOAP api with C# code.

    Thanks

    1. Dear Vinay,

      (1) Use the SOAP Web Service URL for ServiveNow https://.service-now.com/incident.do?SOAP

      (2) In your Visual Studio project, add a web service reference to the URL mentioned in step (1) above

      (3) Create an object of the added web service’s proxy class as -> var serviceResponse = new .ServiceNow_incident();

      (4) Create an object as -> var reqInsert = new .insert();

      (5) Set ‘reqInsert’ object’s properties like – description, short_description, contact_type, subcategory, assignment_group, impact, caller_id, cmdb_ci, priority

      (6) Set credentials of ‘serviceResponse’ object (created in step (3)) as -> serviceResponse.Credentials = new NetworkCredential(“username”, “password”);

      (7) Create the incident by calling ‘serviceResponse’ object’s (created in step (3)) ‘insert’ method and passing the ‘reqInsert’ object (created in step (4)) as -> var getResponse = serviceResponse.insert(reqInsert);

      (8) This will create a new incident. You can use the code in Step (7) calling other methods of ‘serviceResponse’ object to update and delete incident

      Please feel free to post if you have further queries or any other comment.

  5. This example worked great I was wondering if you have an example for retrieving list of incidents based off of summary or by incident or really any criteria. We’re using a rest api profile with a getincident.

  6. IT SHOWING THIS ERROR WHAT CAN I DO”SNOW Incident Number : The remote server returned an error: (400) Bad Request”.

    1. A 400 means that the request was malformed. In other words, the data stream sent by the client to the server didn’t follow the rules.

      In the case of a REST API with a JSON payload, 400’s are typically used to indicate that the JSON is invalid in some way according to the API specification for the service.

    1. Hi Afia,

      Please refer to code snippet of Logger.cs class provided my me in this article. The short description is set in method ‘CreateIncidentServiceNow’ at line number 32. IncidentNumber is fetched at line number 53. Similarly you can fetch ‘Short Description’.

      By categorizing emails based on issue short description, do you mean to generate some report in ServiceNow front end or you want to do this programmatically by writing code?

      If this wasn’t your query, please explain your requirement in more details.

      Thanks and regards,
      Sudipta Chaudhari

  7. c sharp code will read the email and create incident ticket no for the subject what their problem of the email.(incident no with subject of the mail I need).

Leave a Reply