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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Help: OData Date Field in Filter Condition does not work for me

(0) ShareShare
ReportReport
Posted on by 210

Hi Guys,

I'm not sure what I'm doing incorrect in my Rest API call. I want to use the datefield ( date of birth of lead) in Odata query. but the below code does not work for me. can please somebody help me with this.

function gettheDuplicateLead{

var dateofbirth = Xrm.Page.getAttribute("tht_dateofbirth");

var odataDateFormat = getODataUTCDateFilter(dateofbirth.getValue());

retrieveResult.open("GET", ODataPath +"/LeadSet?$select=FullName,LeadId&$filter=(LastName eq '" + lastnamevalue +"' and tht_DateOfBirth eq '" + odataDateFormat +"')",false);
       retrieveResult.setRequestHeader("Accept", "application/json");
       retrieveResult.setRequestHeader("Content-Type", "application/json; charset=utf-8?");
       retrieveResult.send();
         debugger;    
       if (retrieveResult.readyState == 4 /* complete */)
       {
           if (retrieveResult.status == 200)
           {
           var retrieved = this.parent.JSON.parse(retrieveResult.responseText).d;
    var Result = retrieved.results;
     if (typeof Result !== "undefined" && currentleadid == "")
     {
                    for (var i = 0; i < Result.length; i++) {
                   Xrm.Page.ui.setFormNotification('Duplicate Lead - ' +Result[i].FullName+ ' - is found', 'ERROR', notificationMessage);                             
eventArgs.preventDefault();
                   eventArgs.returnValue = false;
       

   }   
    }
        }

}

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 hourString = date.getUTCHours().toString();
   if (hourString.length == 1)
       hourString = "0" + hourString;
   var minuteString = date.getUTCMinutes().toString();
   if (minuteString.length == 1)
       minuteString = "0" + minuteString;
   var secondString = date.getUTCSeconds().toString();
   if (secondString.length == 1)
       secondString = "0" + secondString;
   var DateFilter = "datetime'";
   DateFilter += date.getUTCFullYear() + "-";
   DateFilter += monthString + "-";
   DateFilter += dateString;
   DateFilter += "T" + hourString + ":";
   DateFilter += minuteString + ":";
   DateFilter += secondString + "Z'";
   return DateFilter;
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    Hello Suganthar,

    As far as i can see from your code, it does not have the milliseconds specified.

    Check this code from Microsoft:

    msdn.microsoft.com/.../gg328025.aspx

    Let us know if it works.

    Cheers!

    Pedro Pisco

  • Suganthar_Satkunam Profile Picture
    210 on at

    Hello Pedro, Thanks for your reply.

    I added the milliseconds in the function. but still it does not work.

    When I was alerting the date (odataDateFormat) in the UTC format before passing to Odata filter, it displays as below

    6866.1.PNG

    My Odata query is as below

    retrieveResult.open("GET", ODataPath +"/LeadSet?$select=FullName,LeadId&$filter=(EMailAddress1 eq '" + emailaddress +"') or (Telephone1 eq '" + mobilevalue +"' or MobilePhone eq '" + mainphonevalue +"') or (Telephone1 eq '" + mainphonevalue +"' or MobilePhone eq '" + mobilevalue +"') or (LastName eq '" + lastnamevalue +"' and tht_DateOfBirth eq '" + odataDateFormat +"')",false);

    Do you think it could be related to single quotes and double quotes issue.

  • Verified answer
    Community Member Profile Picture
    on at

    Hello Suganthar,

    I noticed that you are using the OrganizationData, i thought you were using the Web Api.

    Have you tried this?

    missdynamicscrm.blogspot.pt/.../crm-2011-crm-2013-odata-error-when.html

    Might be the solution for you, since your date time field does not contains hours, and it might need two characters.

    Cheers!

    Pedro Pisco

  • Suganthar_Satkunam Profile Picture
    210 on at

    Hello Pedro, Thanks for the reply. Yes I tried it and it did not work. Finally, I figured it out what was an issue. My Date of Birth field on Lead form is a custom field and store the value as date only. that is why the above function did not work in Odata filtering. I managed to get it working. but your suggestion above was helpful to identify the issue

  • Community Member Profile Picture
    on at

    Hello Satkunam,

    Great, i'm glad you solved it.

    And thanks for sharing, will be helpful for us. :)

    Cheers!

    Pedro Pisco

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans