Thursday 24 November 2016

Retrieve Records using SDK.REST

Retrieve All Records using SDK.REST


Hi all,

In my earlier blog, I describe how to retrieve data using SDK.Rest. But in this scenario if we want to retrieve records based on dynamic date then we will face Format issue.

In OdataQuery Designer, Date Format will be like YYYY-MM-DDTHH:MM:SS But while retrieve today's date using Javascript i.e var today = new Date(); Then Date format will be like Fri Nov 25 12:14:18 UTC+0530 2016 So again it will be problem. So we need to convert Current Date to OdataFormater. For that I created one function which will convert normal date to ODATA Format Date.

Here I am retrieving all Account entity records(Including all field values) which are having NominalDate (This is my Custom Date field) as in future. (Greater than current date).

Here I mentioned the code.




Code

function RetrieveAllRecords(context) {
    // Retrieving Current date
    var today = new Date();
    var odataDateFormat = ODataFormatter(today);

    var options = "$select=*&$filter=new_NormalDate ge datetime'" + odataDateFormat + "'";      // My Custom condition for retrieve nominal date as future date
   
    SDK.REST.retrieveMultipleRecords("eps_AustralianNetworkProvider", options, retrieveAccountsCallBack, errorCallBack, ActiveRetrieveComplete);
}

function retrieveAccountsCallBack(retrieveAccountsRecord, context) {
    alert(retrieveAccountsRecord.length);
}
function errorCallBack() { }
function ActiveRetrieveComplete() { }

function ODataFormatter(date) {
    var MonthStr = date.getUTCMonth();
    var DateStr = date.getUTCMonth();
    var HoursStr = date.getUTCMonth();
    var MinStr = date.getUTCMonth();
    var SecStr = date.getUTCMonth();
    if (MonthStr.length == 1)
        MonthStr = "0" + MonthStr;
    if (DateStr.length == 1)
        DateStr = "0" + DateStr;
    if (HoursStr.length == 1)
        HoursStr = "0" + HoursStr;
    if (MinStr.length == 1)
        MinStr = "0" + MinStr;
    if (SecStr.length == 1)
        SecStr = "0" + SecStr;
    var ODateDataFormat = new String();
    ODateDataFormat = date.getUTCFullYear() + "-";
    ODateDataFormat += MonthStr + "-";
    ODateDataFormat += DateStr + "T";
    ODateDataFormat += HoursStr + ":";
    ODateDataFormat += MinStr + ":";
    ODateDataFormat += SecStr;
    return ODateDataFormat;
}


This is the code. You need to add this code in Form Properties. And you need to download SDK.Rest Javascript liabrary and that file also you need to add in your Form Properties.

For download SDK.Rest Liabrary, you can visit this link.

Hope it helps you guys.

Thanks,
Shivaram.