Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 82002

Dynamics CRM WebAPI: Date in Filter condition

$
0
0

Hi Team,

When I was querying contacts data with the date filter using the CRMRest Builder, I got the response without having any issue.

Note we store the date as 'YYYY-MM-DD' in birthdate field.

the result I got is as below

Then I have placed this code and parameterised the date in the webresource and tried it it did not work

if (mainPhone.getValue() != null || mobile.getValue() != null || email.getValue() != null)
    {
      var mainphonevalue= mainPhone.getValue();

      var mobilevalue=mobile.getValue();
  
      var emailaddress = email.getValue();

      var lastnamevalue=lastname.getValue();
    

      var odataDateFormat = formatDate(dateofbirth.getValue());
      alert(odataDateFormat);    
      debugger;
      var req = new XMLHttpRequest();
      req.open("GET", ClientURL + "/api/data/v8.2/contacts?$select=fullname,contactid&$filter=(birthdate eq odataDateFormat)", true);
      req.setRequestHeader("OData-MaxVersion", "4.0");
      req.setRequestHeader("OData-Version", "4.0");
      req.setRequestHeader("Accept", "application/json");
      req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
      req.setRequestHeader("Prefer", "odata.include-annotations=\"*\",odata.maxpagesize=10");
      req.onreadystatechange = function() {
      if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var contactid = results.value[i]["contactid"];
                alert(contactid);
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
   };
   req.send();
    }
}


function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}

Then I hardcoded the parameterised value in javascript and tried it. it worked.

req.open("GET", ClientURL + "/api/data/v8.2/contacts?$select=fullname,contactid&$filter=(birthdate eq 1984-06-24)", true);

Can anybody helped me how can I solve this with parametrised date value.


Viewing all articles
Browse latest Browse all 82002

Latest Images

Trending Articles



Latest Images