- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 03:52 AM
Hello there,
Working with the Web Developer in integrating our ServiceNow platform with there WebApp.
As part of the development, we need a batch job that would query a list of records from the ServiceNow table and update curtains values in there WebApp.
But we unable to query the list of records using SNOW Table API for a specific date.
Can you someone help us with the C# code to get a list of records from a table for a specific date or a date range.
C# code the Web Developer has written.
var client = new RestClient(Instanceurl+ "/api/now/table/incident?sysparm_limit=15");
client.Authenticator = new HttpBasicAuthenticator(user, password);
var request = new RestRequest("incident", Method.GET);
request.AddParameter("sys_updated_on", "2020-02-18 19:23:22");
//Works but only when I pass the specific DateTime value.
request.RequestFormat = DataFormat.Json;
IRestResponse response = client.Execute(request);
dynamic _result = JsonConvert.DeserializeObject<RestAPIResult.ChildrenRootObject>(response.Content.ToString());
if (_result.result.Count > 0) //Important to prevent "Object reference not set to an instance of an object" error.
{
Solved! Go to Solution.
- Labels:
-
Integrations
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 08:08 AM
Hi Satish
I have been like 15 years since i worked with C# but it should be possible to you duo to ServiceNow's input.
I would suggest that you use ServiceNows query builder to make you initial queries and then build them in your C# app
These are examples for different inputs for ServiceNow to filter in either periods or "today" as examples
query["sysparm_query"] = "sys_updated_onBETWEENjavascript:gs.dateGenerate('2020-03-01','00:00:00')@javascript:gs.dateGenerate('2020-05-17','00:00:00')";
query["sysparm_query"] = "sys_updated_on>=javascript:gs.beginningOfToday()";
And then:
Below is a C# sample that should return the correct incidents from a given period or date of these examples
using System;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Collections.Generic;
namespace cs_test
{
internal class Program
{
internal static void Main(string[] args)
{
byte[] byteArray = Encoding.ASCII.GetBytes("username:password");
UriBuilder builder = new UriBuilder("https://instance.service-now.com/api/now/table/incident");
var query = HttpUtility.ParseQueryString(builder.Query);
query["sysparm_limit"] = "15";
query["sysparm_fields"] = "number,short_description";
query["sysparm_query"] = "sys_updated_onBETWEENjavascript:gs.dateGenerate('2020-03-01','00:00:00')@javascript:gs.dateGenerate('2020-05-17','00:00:00')";
//query["sysparm_query"] = "sys_updated_on>=javascript:gs.beginningOfToday()";
builder.Query = query.ToString();
string url = builder.ToString();
Console.WriteLine(url);
GetRequst(url, byteArray);
Console.ReadKey();
}
async static void GetRequst(string url, byte[] byteArray){
using (HttpClient client = new HttpClient()){
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
using (HttpResponseMessage response = await client.GetAsync(url)){
using(HttpContent content = response.Content){
string myContent = await content.ReadAsStringAsync();
var records = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(myContent);
Console.WriteLine(records.result.Count);
foreach(Record rec in records.result){
Console.WriteLine(rec.number + " - " + rec.short_description);
}
}
}
}
}
}
internal class RootObject{
public List<Record> result { get; set; }
}
internal class Record{
public string number { get; set; }
public string short_description { get; set; }
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2020 08:58 AM
I would recommend creating your own Scripted REST API (System Web Services > Scripted Web Service > Scripted REST API) to give you more control on what query parameters your API need and what data would you want it to return and it what format.
Here's a sample. In this sample, the query would require the table name and incident number then return some data:
Response Body:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2020 11:11 AM
I am having problem with executing the Console.ReadKey(); code from task scheduler. Does anybody have a solution how to process this request? Thanks
GetRequest(url,byteArray);
Console.ReadKey();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2020 10:04 PM
Hi Sema
Console.ReadKey() is used to track user inputs so its properly not needed for your solution
https://docs.microsoft.com/en-us/dotnet/api/system.console.readkey?view=netcore-3.1
I used it in the example cause i used Visual studio code for the example
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2020 09:12 AM
Thats what I thought but we were unable to execute this method without this line. My co-worker found a way to run the RESTAPI query for date range with simple method if it helps anybody. Thank you for your help and support Simon.
var client = new RestClient(APIURL + "/api/now/table/<table Name>?");
client.Authenticator = new HttpBasicAuthenticator(<username>, <Password>);
var request = new RestRequest("alm_asset", Method.GET);
// request.AddParameter("sysparm_limit", MaxRows);
request.AddParameter("sysparm_query", "sys_updated_on>=javascript:gs.beginningOfToday(0)");
IRestResponse response = client.Execute(request);
dynamic _result = JsonConvert.DeserializeObject<RestAPIResult.ChildrenRootObject>(response.Content.ToString());
if (_result.result.Count > 0) {}