locked
Using Rest Retrive Lead On Date but not getting result RRS feed

  • Question

  • I want to retrive lead by date using REST but it is not show result by date i used the O Data Query Designer tool to build query

    see the code

    function nextProspect() {
    var query="/LeadSet?$filter=StateCode/Value eq 0 and new_ScheuledDate eq datetime'2013-07-22T12:00:00'"
    fnbuildODataCall(query);
    }
    function fnbuildODataCall(query) {
    var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
    var retrieveReq = new XMLHttpRequest();
    var Odata = oDataPath + query;
    retrieveReq.open("GET", Odata, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveReq.onreadystatechange = function () { RetrieveReqCallBack(this); };
    retrieveReq.send();
    }
    function RetrieveReqCallBack(retrieveReq) {
    var metricId = null;
     if (retrieveReq.readyState == 4 /* complete */) {
      var retrieved = JSON.parse(retrieveReq.responseText).d;
         if (retrieved.results.length > 0) {
            /*if the data is returned include your logic here*/
            /*e.g: var name = retrieved.results[0].Name;*/
              var name=retrieved.results[0].FirstName;
    alert(name);
           }
        }
    }


    Muhammad Sohail

    Monday, July 22, 2013 7:30 AM

Answers

  • Hi Sohail,

    Please use the below function to convert Date field value to UTC Format.

    function getODataUTCDateFilter(date) {
    
     var monthString;
     var rawMonth = (date.getUTCMonth()+1).toString();
     if (rawMonth.length == 1) {
      monthString = "0" + rawMonth;
     }
     else
     { monthString = rawMonth; }
    
     var dateString;
     var rawDate = date.getUTCDate().toString();
     if (rawDate.length == 1) {
      dateString = "0" + rawDate;
     }
     else
     { dateString = rawDate; }
    
    
     var DateFilter = "datetime\'";
     DateFilter += date.getUTCFullYear() + "-";
     DateFilter += monthString + "-";
     DateFilter += dateString;
     DateFilter += "T" + date.getUTCHours() + ":";
     DateFilter += date.getUTCMinutes() + ":";
     DateFilter += date.getUTCSeconds() + ":";
     DateFilter += date.getUTCMilliseconds();
     DateFilter += "Z\'";
     return DateFilter;
    }


    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT

    • Marked as answer by sohail450 Monday, July 22, 2013 10:02 AM
    Monday, July 22, 2013 7:54 AM

All replies

  • Hi Sohail,

    Please use the below function to convert Date field value to UTC Format.

    function getODataUTCDateFilter(date) {
    
     var monthString;
     var rawMonth = (date.getUTCMonth()+1).toString();
     if (rawMonth.length == 1) {
      monthString = "0" + rawMonth;
     }
     else
     { monthString = rawMonth; }
    
     var dateString;
     var rawDate = date.getUTCDate().toString();
     if (rawDate.length == 1) {
      dateString = "0" + rawDate;
     }
     else
     { dateString = rawDate; }
    
    
     var DateFilter = "datetime\'";
     DateFilter += date.getUTCFullYear() + "-";
     DateFilter += monthString + "-";
     DateFilter += dateString;
     DateFilter += "T" + date.getUTCHours() + ":";
     DateFilter += date.getUTCMinutes() + ":";
     DateFilter += date.getUTCSeconds() + ":";
     DateFilter += date.getUTCMilliseconds();
     DateFilter += "Z\'";
     return DateFilter;
    }


    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT

    • Marked as answer by sohail450 Monday, July 22, 2013 10:02 AM
    Monday, July 22, 2013 7:54 AM
  • I think it is for current date but i want to retrive records other than current date.

    and where from date parameter as you pass in function getODataUTCDateFilter(date)?

    or can you adjust your code to my code to be final code .........


    Muhammad Sohail


    • Edited by sohail450 Monday, July 22, 2013 8:19 AM Changing
    Monday, July 22, 2013 8:16 AM
  • Hi Sohail,

    Just change your query like below.And it will return all the leads whose new_scheduleddate<today

    var today=new Date();

    var query="/LeadSet?$filter=StateCode/Value eq 0 and new_ScheuledDate le datetime'"+ getODataUTCDateFilter(today)+"'";


    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful" Thank you, DT

    Monday, July 22, 2013 8:46 AM