I have just started writing some C# code for accessing data via the Service Now REST API.
This was not particularly difficult, but I did find a small gotcha when it came to more complex queries.
After consuming the Web Service in Visual Studio, queries like the following worked no problem:
getRecords.sys_created_on = ">= 2020/07/10";
getRecords.__encoded_query = "sys_created_on >= 2020/07/01 00:00:00 ";
But something like this would not:
getRecords.sys_created_on = "BETWEEN 2019-04-04 00:00:00 AND 2020-04-14 11:59:49";
The online documentation refers to creating a query in the Service Now web application and right clicking and selecting “Copy query”. This results in something like:
sys_created_onBETWEENjavascript:gs.dateGenerate(‘2020-07-01′,’00:00:00’)@javascript:gs.dateGenerate(‘2020-07-10′,’23:59:59’)
Initially I thought I had to copy the date format the Java Script had created, but actually that is not how to use it. This is the correct way:
getRecords.__encoded_query = "sys_created_onBETWEENjavascript:gs.dateGenerate('2020-07-01', '00:00:00')@javascript: gs.dateGenerate('2020-07-10', '23:59:59')";
See this link.
The full code to fetch a range of records in C# would look like this:
SNOW1.Snow.ServiceNowSoapClient soapClient = new SNOW1.Snow.ServiceNowSoapClient(); soapClient.ClientCredentials.UserName.UserName = "username"; soapClient.ClientCredentials.UserName.Password = "password"; Snow.getRecords getRecords = new Snow.getRecords(); getRecords.__encoded_query = "sys_created_onBETWEENjavascript:gs.dateGenerate('2020-07-01', '00:00:00')@javascript: gs.dateGenerate('2020-07-10', '23:59:59')"; Snow.getRecordsResponseGetRecordsResult[] records = soapClient.getRecords(getRecords); try { foreach (Snow.getRecordsResponseGetRecordsResult response in records) { this.richTextBoxResult.Text += "Incident Number: " + response.number + "\n"; this.richTextBoxResult.Text += "Opened At: " + response.opened_at + "\n"; this.richTextBoxResult.Text += "Created: " + response.sys_created_on + "\n"; this.richTextBoxResult.Text += "Sys_id: " + response.sys_id + "\n"; this.richTextBoxResult.Text += "short_description: " + response.short_description + "\n"; this.richTextBoxResult.Text += "\n"; } } catch (Exception error) { this.richTextBoxResult.Text = error.Message; }