- 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 04:05 AM
i think in C# you can pass the below line to get the date time and then pass it.
DateTime now = DateTime.Now;
i think this way it should work. also add log to check the now value .
request.AddParameter("sys_updated_on", now);
- 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 09:00 AM
Awesome!! worked like a champ.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2020 08:54 AM
Simon, your solution worked. Thank you so much for sharing this information