{"id":26,"date":"2020-07-10T15:56:56","date_gmt":"2020-07-10T05:56:56","guid":{"rendered":"http:\/\/gerardwknight.duckdns.org\/?page_id=26"},"modified":"2020-07-13T16:54:37","modified_gmt":"2020-07-13T06:54:37","slug":"creating-complex-queries-in-c-for-rest-access-to-service-now","status":"publish","type":"page","link":"https:\/\/gerardwknight.duckdns.org\/index.php\/creating-complex-queries-in-c-for-rest-access-to-service-now\/","title":{"rendered":"Creating Complex Queries in C# For REST Access To Service Now"},"content":{"rendered":"\n<p>I have just started writing some C# code for accessing data via the Service Now REST API. <\/p>\n\n\n\n<p>This was not particularly difficult, but I did find a small gotcha when it came to more complex queries.<\/p>\n\n\n\n<p>After consuming the Web Service in Visual Studio, queries like the following worked no problem:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getRecords.sys_created_on = \"&gt;= 2020\/07\/10\";\ngetRecords.__encoded_query = \"sys_created_on &gt;= 2020\/07\/01 00:00:00 \";<\/code><\/pre>\n\n\n\n<p>But something like this would not:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getRecords.sys_created_on = \"BETWEEN 2019-04-04 00:00:00 AND 2020-04-14 11:59:49\";<\/code><\/pre>\n\n\n\n<p>The online documentation refers to creating a query in the Service Now web application and right clicking and selecting &#8220;Copy query&#8221;. This results in something like:<\/p>\n\n\n\n<p>sys_created_onBETWEENjavascript:gs.dateGenerate(&#8216;2020-07-01&#8242;,&#8217;00:00:00&#8217;)@javascript:gs.dateGenerate(&#8216;2020-07-10&#8242;,&#8217;23:59:59&#8217;)<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>getRecords.__encoded_query = \"sys_created_onBETWEENjavascript:gs.dateGenerate('2020-07-01', '00:00:00')@javascript: gs.dateGenerate('2020-07-10', '23:59:59')\";<\/code><\/pre>\n\n\n\n<p>See this <a href=\"http:\/\/docs.servicenow.com\/bundle\/orlando-platform-user-interface\/page\/use\/using-lists\/concept\/c_EncodedQueryStrings.html\">link<\/a>. <\/p>\n\n\n\n<p>The full code to fetch a range of records in C# would look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SNOW1.Snow.ServiceNowSoapClient soapClient = new SNOW1.Snow.ServiceNowSoapClient();\nsoapClient.ClientCredentials.UserName.UserName = \"username\";\nsoapClient.ClientCredentials.UserName.Password = \"password\";\n\nSnow.getRecords getRecords = new Snow.getRecords();\n\ngetRecords.__encoded_query = \"sys_created_onBETWEENjavascript:gs.dateGenerate('2020-07-01', '00:00:00')@javascript: gs.dateGenerate('2020-07-10', '23:59:59')\";\n\n\nSnow.getRecordsResponseGetRecordsResult[] records = soapClient.getRecords(getRecords);\n\n try\n {\n    foreach (Snow.getRecordsResponseGetRecordsResult response in records)\n    {\n        this.richTextBoxResult.Text += \"Incident Number: \" + response.number + \"\\n\";\n        this.richTextBoxResult.Text += \"Opened At: \" + response.opened_at + \"\\n\";\n        this.richTextBoxResult.Text += \"Created: \" + response.sys_created_on + \"\\n\";\n        this.richTextBoxResult.Text += \"Sys_id: \" + response.sys_id + \"\\n\";\n        this.richTextBoxResult.Text += \"short_description: \" + response.short_description +  \"\\n\";\n        this.richTextBoxResult.Text +=  \"\\n\";\n    }\n\n }\n catch (Exception error)\n {\n     this.richTextBoxResult.Text = error.Message;\n }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 = &#8220;&gt;= 2020\/07\/10&#8221;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Complex Queries in C# For REST Access To Service Now - GerardWKnight<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/gerardwknight.duckdns.org\/index.php\/creating-complex-queries-in-c-for-rest-access-to-service-now\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Complex Queries in C# For REST Access To Service Now - GerardWKnight\" \/>\n<meta property=\"og:description\" content=\"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 = &quot;&gt;= 2020\/07\/10&quot;; [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/gerardwknight.duckdns.org\/index.php\/creating-complex-queries-in-c-for-rest-access-to-service-now\/\" \/>\n<meta property=\"og:site_name\" content=\"GerardWKnight\" \/>\n<meta property=\"article:modified_time\" content=\"2020-07-13T06:54:37+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/gerardwknight.duckdns.org\/#website\",\"url\":\"http:\/\/gerardwknight.duckdns.org\/\",\"name\":\"GerardWKnight\",\"description\":\"Web site of Melbourne based developer Gerard Knight\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/gerardwknight.duckdns.org\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/gerardwknight.duckdns.org\/index.php\/creating-complex-queries-in-c-for-rest-access-to-service-now\/#webpage\",\"url\":\"http:\/\/gerardwknight.duckdns.org\/index.php\/creating-complex-queries-in-c-for-rest-access-to-service-now\/\",\"name\":\"Creating Complex Queries in C# For REST Access To Service Now - GerardWKnight\",\"isPartOf\":{\"@id\":\"http:\/\/gerardwknight.duckdns.org\/#website\"},\"datePublished\":\"2020-07-10T05:56:56+00:00\",\"dateModified\":\"2020-07-13T06:54:37+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/gerardwknight.duckdns.org\/index.php\/creating-complex-queries-in-c-for-rest-access-to-service-now\/\"]}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/pages\/26"}],"collection":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":7,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/pages\/26\/revisions"}],"predecessor-version":[{"id":39,"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/pages\/26\/revisions\/39"}],"wp:attachment":[{"href":"https:\/\/gerardwknight.duckdns.org\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}