{"id":3555,"date":"2019-04-04T05:23:48","date_gmt":"2019-04-04T05:23:48","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=3555"},"modified":"2019-04-04T05:52:15","modified_gmt":"2019-04-04T05:52:15","slug":"fetching-data-based-on-date-field-using-webapi","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=3555","title":{"rendered":"Fetching data based on date field using WebAPI"},"content":{"rendered":"<p><strong>Introduction<\/strong><br \/>\nAre you looking for sample code to fetch data from Dynamics 365 CE using WebAPI ? if yes this post will help. We are discussing how to fetch data based on the datetime field from account entity.<\/p>\n<p>Look for our other WebAPI Samples <a href=\"https:\/\/himbap.com\/blog\/?page_id=2710\">here<\/a><\/p>\n<p><strong>Requirement<\/strong><br \/>\nRetrieve accounts records which created on or before today&#8217;s date.<\/p>\n<p><strong>Solution<\/strong><br \/>\nWrite writing WebAPI request first thing make sure to check all the available filters that we can use in our WebAPI request, you can get full details of supported <a href=\"https:\/\/docs.microsoft.com\/en-us\/dynamics365\/customer-engagement\/developer\/webapi\/query-data-web-api\">filters here<\/a>.<\/p>\n<p>Now our requirement is to compare data based on the current date, so first thing we need to get current data and format it correctly using libraries like <strong>Moment.js<\/strong> or you can write your own javascript code to format like following.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction FormatDate() {\r\n    var today = new Date();\r\n    var month = today.getMonth() + 1; \/\/to use correct month, it will return 0 for jan\r\n    var day = today.getDate();\r\n    var year = today.getFullYear();\r\n    return month + &quot;-&quot; + day + &quot;-&quot; + year;\r\n}\r\n<\/pre>\n<p>Above function will get current date and will format it to &#8220;mm-dd-yyyy&#8221;, what we need to query Dynamics 365 CE. Now we can use it in our WebAPI request query. In following example I am going to query account entity record which is created today or before. I am counting total number of records, to count number of records we can use <strong>count<\/strong> option in our query.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction retrieveAccounts() {\r\n   \r\n    var currentDate = FormatDate();\r\n    var entity = &quot;accounts&quot;;\r\n    var columnSet = &quot;?$select=name&amp;$filter=createdon le '&quot; + currentDate + &quot;'&amp;$count=true&quot;;\r\n    var serverURL = Xrm.Page.context.getClientUrl();\r\n    var Query = entity + columnSet;\r\n    var req = new XMLHttpRequest();\r\n    req.open(&quot;GET&quot;, serverURL + &quot;\/api\/data\/v9.0\/&quot; + Query, true);\r\n    req.setRequestHeader(&quot;Accept&quot;, &quot;application\/json&quot;);\r\n    req.setRequestHeader(&quot;Content-Type&quot;, &quot;application\/json; charset=utf-8&quot;);\r\n    req.setRequestHeader(&quot;OData-MaxVersion&quot;, &quot;4.0&quot;);\r\n    req.setRequestHeader(&quot;OData-Version&quot;, &quot;4.0&quot;);\r\n  \r\n    req.onreadystatechange = function() {\r\n        if (this.readyState == 4 \/* complete *\/ ) {\r\n            req.onreadystatechange = null;\r\n            if (this.status == 200) {\r\n                var data = JSON.parse(this.response);\r\n                if (data['@odata.count'] != null)\r\n                    alert(&quot;Total Records:&quot;+ data['@odata.count']); \r\n            } else {\r\n                var error = JSON.parse(this.response).error;\r\n                alert(error.message);\r\n            }\r\n        }\r\n    };\r\n    req.send();\r\n}\r\n<\/pre>\n<p><strong>Summary<\/strong><br \/>\nWe learned how to format date using java script code and learned how to use formatted date in WebAPI request. You can use above code in your web resource and can do required changes.<\/p>\n<p>Hope it will help someone !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Are you looking for sample code to fetch data from Dynamics 365 CE using WebAPI ? if yes this post will help. We are discussing how to fetch data based on the datetime field from account entity. Look for our other WebAPI Samples here Requirement Retrieve accounts records which created on or before today&#8217;s date. Solution Write writing WebAPI&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=3555\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":3036,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[402],"tags":[702,700,698,699,701],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3555"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3555"}],"version-history":[{"count":6,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3555\/revisions"}],"predecessor-version":[{"id":3562,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3555\/revisions\/3562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/media\/3036"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}