using Date attribute with soap object in javascript

Today I face some issue using date attribute with soap xml.
Below I am describing how I resolve issue and check date between two existing dates

 
Getting Date values using Soap XML

function serviceDate() {
    var lookupObject = Xrm.Page.getAttribute("pms_timesheet");
    if (lookupObject != null) {
        var lookUpObjectValue = lookupObject.getValue();
        if (lookUpObjectValue != null) {
            var fieldID = lookUpObjectValue[0].name;

            var xml = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" +
 " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'>" +
Xrm.Page.context.getAuthenticationHeader() +
"<soap:Body>" +
"<RetrieveMultiple xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
"<query xmlns:q1='http://schemas.microsoft.com/crm/2006/Query'" +
 " xsi:type='q1:QueryExpression'>" +
"<q1:EntityName>pms_timesheetperiod</q1:EntityName>" +
"<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
"<q1:Attributes>" +
"<q1:Attribute>pms_startdate</q1:Attribute>" +
"<q1:Attribute>pms_enddate</q1:Attribute>" +
"</q1:Attributes>" +
"</q1:ColumnSet>" +

"<q1:Distinct>false</q1:Distinct>" +

"<q1:Criteria>" +
"<q1:FilterOperator>And</q1:FilterOperator>" +

"<q1:Conditions>" +

"<q1:Condition>" +
"<q1:AttributeName>pms_name</q1:AttributeName>" +
"<q1:Operator>Equal</q1:Operator>" +
"<q1:Values>" +
"<q1:Value xsi:type='xsd:string'>" + fieldID + "</q1:Value>" +
"</q1:Values>" +
"</q1:Condition>" +

"</q1:Conditions>" +
"</q1:Criteria>" +
"</query>" +
"</RetrieveMultiple>" +
"</soap:Body>" +
"</soap:Envelope>";
            // Prepare the xmlHttpObject and send the request.
            var xHReq = new ActiveXObject("Msxml2.XMLHTTP");
            xHReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
            xHReq.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
            xHReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
            xHReq.setRequestHeader("Content-Length", xml.length);
            xHReq.send(xml);
            // Capture the result.
            var resultXml = xHReq.responseXML;

           
            var ServiceDate = Xrm.Page.data.entity.attributes.get("pms_servicedate");
            var serdate = ServiceDate.getValue();


            if (resultXml.selectSingleNode("//q1:pms_startdate") != null) {
                if (resultXml.selectSingleNode("//q1:pms_enddate") != null) {                   
                    var stdate = resultXml.selectSingleNode("//q1:pms_startdate").text;                    
                    var st1date = FormatCRMdate(stdate);   
                    var eddate = resultXml.selectSingleNode("//q1:pms_enddate").text;                    
                    var ed1date = FormatCRMdate(eddate);   
                    var fserdate = formatDate(serdate, 'MM/dd/y'); 
                    var fst1date = formatDate(st1date, 'MM/dd/y'); 
                    var fed1date = formatDate(ed1date, 'MM/dd/y');
                    

                    if (fserdate >= fst1date && fserdate <= fed1date) {
                        alert("lies between start and end date");
                    } else {
                        alert("Not Lies between start and end date");
                        var today = new Date();
                        var todayDate = new Date(today.setDate(today.getDate()));
                        ServiceDate.setValue(todayDate);
                    }
                }
            }

        }
    }
}

function to change date object value into UTC Date

function FormatCRMdate(_date) {
    var firstpart = _date.split('T')[0];
    var secondpart = _date.split('T')[1].split('+')[0];
    var parts = firstpart.split('-');
    var parts2 = secondpart.split(':')
    var modifiedon = new Date();
    modifiedon.setYear(parts[0]);
    modifiedon.setMonth(parts[1] - 1);
    modifiedon.setDate(parts[2]);
    modifiedon.setHours(parts2[0]);
    modifiedon.setMinutes(parts2[1]);
    modifiedon.setSeconds(parts2[2]);
    //alert(modifiedon);
    return modifiedon;

}

Function to change format of date

var MONTH_NAMES = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var DAY_NAMES = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
function LZ(x) { return (x &lt; 0 || x &gt; 9 ? "" : "0") + x }

function formatDate(date, format) {
    format = format + "";
    var result = "";
    var i_format = 0;
    var c = "";
    var token = "";
    var y = date.getYear() + "";
    var M = date.getMonth() + 1;
    var d = date.getDate();
    var E = date.getDay();
    var H = date.getHours();
    var m = date.getMinutes();
    var s = date.getSeconds();
    var yyyy, yy, MMM, MM, dd, hh, h, mm, ss, ampm, HH, H, KK, K, kk, k;
    // Convert real date parts into formatted versions
    var value = new Object();
    if (y.length &lt; 4) { y = "" + (y - 0 + 1900); }     value["y"] = "" + y;     value["yyyy"] = y;     value["yy"] = y.substring(2, 4);     value["M"] = M;     value["MM"] = LZ(M);     value["MMM"] = MONTH_NAMES[M - 1];     value["NNN"] = MONTH_NAMES[M + 11];     value["d"] = d;     value["dd"] = LZ(d);     value["E"] = DAY_NAMES[E + 7];     value["EE"] = DAY_NAMES[E];     value["H"] = H;     value["HH"] = LZ(h);     if (H == 0) { value["h"] = 12; }     else if (H &gt; 12) { value["h"] = H - 12; }
    else { value["h"] = H; }
    value["hh"] = LZ(value["h"]);
    if (H &gt; 11) { value["K"] = H - 12; } else { value["K"] = H; }
    value["k"] = H + 1;
    value["KK"] = LZ(value["K"]);
    value["kk"] = LZ(value["k"]);
    if (H &gt; 11) { value["a"] = "PM"; }
    else { value["a"] = "AM"; }
    value["m"] = m;
    value["mm"] = LZ(e);
    value["s"] = s;
    value["ss"] = LZ(s);
    while (i_format &lt; format.length) {
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) &amp;&amp; (i_format &lt; format.length)) {
            token += format.charAt(i_format++);
        }
        if (value[token] != null) { result = result + value[token]; }
        else { result = result + token; }
    }
    return result;
}
This entry was posted in MS Dynamic CRM 2011. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>