Skip to main content

Notifications

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

  • ACECORP Profile Picture
    1,589 on at
    RE: Anomaly getting a date from one form and setting it on another

    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!

  • ACECORP Profile Picture
    1,589 on at
    RE: Anomaly getting a date from one form and setting it on another

    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
    RE: Anomaly getting a date from one form and setting it on another

    Both date fields are "date only"

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 54

#3
dkrishna Profile Picture

dkrishna 6

Featured topics

Product updates

Dynamics 365 release plans