web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Dynamics CRM WebAPI: Date in Filter condition

(0) ShareShare
ReportReport
Posted on by 210

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.

4186.11.PNG

the result I got is as below

8304.22.png

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.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Andreas Cieslik Profile Picture
    9,267 on at

    in the first line you do not check dateofbirth for null value

    and

    "/api/data/v8.2/contacts?$select=fullname,contactid&$filter=(birthdate eq odataDateFormat)",

    needs to be

    "/api/data/v8.2/contacts?$select=fullname,contactid&$filter=(birthdate eq " + odataDateFormat + ")",

    or more simplified without the braces

    "/api/data/v8.2/contacts?$select=fullname,contactid&$filter=birthdate eq " + odataDateFormat,

  • Suganthar_Satkunam Profile Picture
    210 on at

    Thanks Andreas. The null validation code i have already done and did not include in this question

  • Suggested answer
    Scott Moore Profile Picture
    17 on at

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans