Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

How to copy date and time field values from parent record to child record in cloning record

Posted on by 340

I am cloning record from existing record using Xrm.Utility.openEntityForm. I could unable to populate date and time field value from on form to another form. How can this be done using java script. The below code is not working as expected.


var dtValue = Xrm.Page.getAttribute("test").getValue();

if (dtValue != null && dtValue != " " && dtValue != "undefined")
{
dtValue.setDate(new Date().getDate() + 3);
parameters["test"] = dtValue.format("MM/DD/YYYY");
}

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to copy date and time field values from parent record to child record in cloning record

    Hi Sandy,

    I'm glad that you problem has been solved.

    Please mark as verified if the answer is helpful, it would be greatly appreciated.

    Regards,

    Leo

  • Sandy Hello Profile Picture
    Sandy Hello 340 on at
    RE: How to copy date and time field values from parent record to child record in cloning record

    Thanks for your respnse.  Below code worked for me.

    var date1 = new Date();
    var day1 = null;
    var month1 = null;
    var year1 = null;
    var dateStr = null;
    var tm = null;
    var hours = null;
    var minutes = null;
    var ampm = null;
    var strTime = null;

    if (date1 != null && date1 != " " && date1 != "undefined")
    {

    day1 = date1 .getDate();
    month1 = date1 .getMonth() + 1; // Since getMonth() returns month from 0-11 not 1-12
    year1 = date1 .getFullYear();
    dateStr = month1 + "/" + day1 + "/" + year1;
    hours = date1 .getHours();
    minutes = date1 .getMinutes();
    ampm = hours >= 12 ? 'pm' : 'am';
    hours = hours % 12;
    hours = hours ? hours : 12; // the hour '0' should be '12'
    minutes = minutes < 10 ? '0'+minutes : minutes;
    strTime = hours + ':' + minutes + ' ' + ampm;

    }

    parameters["testdate"] = dateStr + ' ' +strTime;

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to copy date and time field values from parent record to child record in cloning record

    Hi Sandy,

    Do you want to get year, month and date(even with hour and minute) from value in Crm date and time field value and then format them in format you want and set it to child record date and time field?

     

    You could take below code as reference:

     

    var crmDate = Xrm.Page.getAttribute("parent_date").getValue();

     

    var convertDate = new Date(crmDate);

     

    var year = convertDate.getFullYear();

     

    var month = convertDate.getMonth() + 1;

     

    var day = convertDate.getDate();

     

    var hour = convertDate.getHours();

     

    var minute = convertDate.getMinutes();

     

    convertDate.setDate(day + 3);

     

    var newDate1 = month+"/"+convertDate.getDate()+"/"+year;

     

    var newDate2 = month+"/"+convertDate.getDate()+"/"+year+" "+hour+":"+minute;

     

    var newCrmDate = new Date(newDate1);

     

    Xrm.Page.getAttribute("child_date").setValue(newCrmDate);

     

    Please point out if I have any misunderstanding.

    Regards,

    Leo

  • Sandy Hello Profile Picture
    Sandy Hello 340 on at
    RE: How to copy date and time field values from parent record to child record in cloning record

    I am able capture date value from date and time value from parent record while cloning. I want to capture both date and time in MM/DD/YYYY HH:MM format. How this can be achieved

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: How to copy date and time field values from parent record to child record in cloning record

    Hi Sandy,

    It seems that you want to set the child record date field to specific format (Date only),

    you should set or change its display format in the field properties instead of formatting by yourself.

    I suggest you could create a new function to add days, below is my working code, you could take it as reference:

     

    var entity = "contact";

    var windowOptions = {

     openInNewWindow: true

    };

    var date = Xrm.Page.getAttribute("new_mytime").getValue();

    var parameters = {};

    parameters["formid"] = "1fed44d1-ae68-4a41-bd2b-f13acac4acfa";

    parameters["lastname"] = "Test";

    parameters["telephone1"] = "(425) 555-1234";

    parameters["birthdate"] = addDays(date, 3);

    Xrm.Utility.openEntityForm(entity, null, parameters, windowOptions);

     

    function addDays(date, days) {

      var result = new Date(date);

      result.setDate(result.getDate() + days);

      return result;

    }

    09101.png

    Best Regards,

    Leo

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans