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

  • Suggested answer
    Scott Moore Profile Picture
    Scott Moore 15 on at
  • Suganthar_Satkunam Profile Picture
    Suganthar_Satkunam 210 on at
    RE: Dynamics CRM WebAPI: Date in Filter condition

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

  • Verified answer
    Andreas Cieslik Profile Picture
    Andreas Cieslik 9,267 on at
    RE: Dynamics CRM WebAPI: Date in Filter condition

    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,

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,432 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans