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)

SDK REST - Retrieve record with date filter

(0) ShareShare
ReportReport
Posted on by

Hello, 

I want to retrieve only the records greater than today using SDK REST.

Any help is appreciated.Below is the code :

// JavaScript source code :

var datedujour = new Date();

var options = "$filter=abo_produit/Id eq guid'" + productID+ "' and abo_date_expedition gt " + datedujour;

SDK.REST.retrieveMultipleRecords(
"abo_lot",
options,
function (lotCollection) {

},
function (error) {
alert(error.message);
},
function (){
});

*This post is locked for comments

I have the same question (0)
  • Nithya Gopinath Profile Picture
    17,078 on at

    Hi, 

    I think the following code solves the issue. Also refer the link given below.
    https://msdn.microsoft.com/en-us/library/gg309461(v=crm.7).aspx#BKMK_filter 

    // JavaScript source code :
    
    var datedujour = new Date();
    
    var options = "$filter=abo_produit/Id eq guid'" + productID+ "' and abo_date_expedition gt datetime'" +  datedujour.toISOString()+ "'";
    
    SDK.REST.retrieveMultipleRecords(
    
    "abo_lot",
    
    options,
    
    function (lotCollection) {
    
    },
    
    function (error) {
    
    alert(error.message);
    
    },
    
    function (){
    
    });

    Thanks

  • Community Member Profile Picture
    on at

    Hi,

    thanks for your time.

    I had tried you code, and to me it should work. But I have this error :

    capt2.PNG

    And this is my options :

    capt2.PNG

    Thanks.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi PHK,

    This happens when there is syntax error in the query.

    Please refer the links below:

    vjcity.blogspot.in/.../a-binary-operator-with-incompatible.html

    vjcity.blogspot.in/.../operator-ge-incompatible-with-operand.html

    Hope this helps you.

    Thanks

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    Hi,

    I think your Query should be

    var options = "$filter=abo_produit/Id eq guid'" + productID+ "' and abo_date_expedition ge " + datedujour;

    Not gt.

    And check OData Query Fields and Entity Name should be based on Schema name. Not based on Logical Name.

    try this one.

    Hope it helps you.

    Thanks.

  • Suggested answer
    shivaram Profile Picture
    3,315 on at

    Hi,

    First of all I am sorry for my Last and Wrong reply.

    This is because of date Format.

    var today=new Date() will give Result in different Format.

    But OData will take only format like 

    Year - Month - Date T Hours : Min : Sec

    Just Like 2016-11-24T06:36:32

    So use following function for getting that 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 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 = new String();
    DateFilter += date.getUTCFullYear() + "-";
    DateFilter += monthString + "-";
    DateFilter += dateString;
    DateFilter += "T" + hourString + ":";
    DateFilter += minuteString + ":";
    DateFilter += secondString;
    return DateFilter;
    }

    So your code will be like this.

    // JavaScript source code :

    var datedujour = new Date();

    var odataDateFormat =getODataUTCDateFilter(datedujour);

    var options = "$filter=abo_produit/Id eq guid'" + productID+ "' and abo_date_expedition gt " + odataDateFormat ;

    SDK.REST.retrieveMultipleRecords(
    "abo_lot",
    options,
    function (lotCollection) {

    },
    function (error) {
    alert(error.message);
    },
    function (){
    });

    Hope it helps you

    Thanks.

  • Verified answer
    Community Member Profile Picture
    on at

    Hello everybody,

    Thanks for you help.

    I merged your code to find the correct synthax :)

    var datedujour = new Date();

    var odataDateFormat =getODataUTCDateFilter(datedujour);

    var options = "$filter=abo_produit/Id eq guid'" + productID+ "' and abo_date_expedition gt datetime'" + odataDateFormat +"'";

    And it works :)

  • shivaram Profile Picture
    3,315 on at

    Hi PHK,

    If this answer is useful for you, then verify this answer. So it will be useful for others.

    Thanks.

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