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)

Anomaly getting a date from one form and setting it on another

(0) ShareShare
ReportReport
Posted on by 1,589

I am running into a very strange anomaly when I retrieve a date from one form, and set it on another based on the selection of records from a drop down bar.

The date in another form that was saved and set there via the date time picker, in this example, is 6/13/1943

My javascript pulls it and tells me via alert the value is this: /Date(-837979200000)/

My function below processes this and ultimately seeks to set the date in a new date time picker field on this new form.

However, it doesn't set the correct 6/13/1943 date in the new date time picker field and instead sets a bad date of 7/21/1996 which is a far cry from 6/13/1943

If I don't append the date value with  ".match(/\d+/)[0] * 1" I get an error because the data is not in the right format for setting the new date time picker field.

What the heck am I doing wrong?

Any help is greatly appreciated.

function getDateLookupDetails(lField, dsName, gField1, sField1) {
    //lField - the name of the lookup field
    //dsName - the name of the dataset or form that the lookup field links back to
    //gField - the name of the field in the linkback dataset that we want to get data from 
    //sField - the name of the file on the current form where we want to write the value that we pulled from the dataset into 
    var lookUpObjectValue = Xrm.Page.getAttribute(lField).getValue();
    if ((lookUpObjectValue != null)) {
        var lookuptextvalue = lookUpObjectValue[0].name;
        var lookupid = lookUpObjectValue[0].id;
        var serverUrl = Xrm.Page.context.getServerUrl();
        //The XRM OData end-point 
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
        var odataSetName = dsName;
        var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                var result_data = data.d;
                //alert(result_data[gField1]);
                //  Xrm.Page.getAttribute(sField1).setValue(result_data[gField1]);
                if ((result_data[gField1]) != null) { Xrm.Page.getAttribute(sField1).setValue(new Date(result_data[gField1].match(/\d+/)[0] * 1)); }
                else {Xrm.Page.getAttribute(sField1).setValue(null);}
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
        });

    }

}


*This post is locked for comments

I have the same question (0)
  • ACECORP Profile Picture
    1,589 on at

    Both date fields are "date only"

  • ACECORP Profile Picture
    1,589 on at

    Guido, Thanks! - you must have posted this while I was off finding and implementing the same material from that blog I listed below.

    I think I actually found that blog from a dialogue in another area of this CRM forum where YOU were talking about a similar issue so thanks for that! Your answer there and here ultimately solved my problem!

    https://rajeevpentyala.com/2011/08/21/read-odata-service-date-field-in-crm-2011/

    The code that fixed the issue is shown below

    function getDateLookupDetails(lField, dsName, gField1, sField1) {
        //lField - the name of the lookup field
        //dsName - the name of the dataset or form that the lookup field links back to
        //gField - the name of the field in the linkback dataset that we want to get data from 
        //sField - the name of the file on the current form where we want to write the value that we pulled from the dataset into 
        var lookUpObjectValue = Xrm.Page.getAttribute(lField).getValue();
        if ((lookUpObjectValue != null)) {
            var lookuptextvalue = lookUpObjectValue[0].name;
            var lookupid = lookUpObjectValue[0].id;
            var serverUrl = Xrm.Page.context.getServerUrl();
            //The XRM OData end-point 
            var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
            var odataSetName = dsName;
            var odataSelect = serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + lookupid + "')";
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: odataSelect,
                beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
                success: function (data, textStatus, XmlHttpRequest) {
                    var result_data = data.d;
                    //alert(result_data[gField1]);
                    //  Xrm.Page.getAttribute(sField1).setValue(result_data[gField1]);
                    if ((result_data[gField1]) != null) {
                    var dt = result_data[gField1];
                        dt = dt.replace("/Date(", "");
                        dt = dt.replace(")/", "");
                        var dateValue = new Date(parseInt(dt, 10));
                        dateValue.setDate(dateValue.getDate());
                        //Xrm.Page.data.entity.attributes.get("{fld_name}").setValue(dateValue);
                        Xrm.Page.getAttribute(sField1).setValue(dateValue); }
                    else { Xrm.Page.getAttribute(sField1).setValue(null); }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); }
            });
    
        }
    
    }
    
    
  • ACECORP Profile Picture
    1,589 on at

    Guido, Thanks! - you must have posted this while I was off finding and implementing the same material from that blog I listed below.

    I think I actually found that blog from a dialogue in another area of this CRM forum where YOU were talking about a similar issue so thanks for that! Your answer there and here ultimately solved my problem!

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