locked
Populate one date field with same value as another date field RRS feed

  • Question

  • I have two date fields: start date and end date. When a user populates the start date field, I need for the end date to have the same value. How do I do that?

    Also, for another scenario...if the user populates the start date, I also need to add on 1 more day to the end date, e.g, start date = 12/1/2010, the end date needs to read: 12/3/2010. How can I do this?

    THANK YOU


    Apps
    Monday, December 20, 2010 2:45 PM

Answers

  • I have two date fields: start date and end date. When a user populates the start date field, I need for the end date to have the same value. How do I do that?

    Try this

    crmForm.all.<EndDate>.DataValue=new Date(crmForm.all.<StartDate>.DataValue);

    Also, for another scenario...if the user populates the start date, I also need to add on 1 more day to the end date, e.g, start date = 12/1/2010, the end date needs to read: 12/3/2010. How can I do this?

    Try this

    crmForm.all.<EndDate>.DataValue=new Date(crmForm.all.<StartDate>.DataValue).getDate()+1;


    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 2:57 PM
    Moderator
  • Sorry you need to use below code for second requirement

    var _Date=new Date(crmForm.all.<StartDate>.DataValue);
    var _Day=_Date.getDate();
    _Date.setDate(_Day+1);
    crmForm.all.<EndDate>.DataValue=_Date;


    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 3:16 PM
    Moderator

All replies

  • I have two date fields: start date and end date. When a user populates the start date field, I need for the end date to have the same value. How do I do that?

    Try this

    crmForm.all.<EndDate>.DataValue=new Date(crmForm.all.<StartDate>.DataValue);

    Also, for another scenario...if the user populates the start date, I also need to add on 1 more day to the end date, e.g, start date = 12/1/2010, the end date needs to read: 12/3/2010. How can I do this?

    Try this

    crmForm.all.<EndDate>.DataValue=new Date(crmForm.all.<StartDate>.DataValue).getDate()+1;


    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 2:57 PM
    Moderator
  • Hi, Mahender...I'll try both.

    Thank you.


    Apps
    Monday, December 20, 2010 3:01 PM
  • Sorry you need to use below code for second requirement

    var _Date=new Date(crmForm.all.<StartDate>.DataValue);
    var _Day=_Date.getDate();
    _Date.setDate(_Day+1);
    crmForm.all.<EndDate>.DataValue=_Date;


    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 3:16 PM
    Moderator
  • Thanks, Mahender. I was just going to reply that the previous code did not work for adding a day. Thanks! This, however, works great!

    Question: These date changes will only occur when the user chooses a specific value from a lookup field. In my 'If
    statement, will I need to put in a lookupitemid, name, typename? I believe I have another post that deals with trying to retrieve a value from a lookup field. It is more code than a picklist value. Right?

    e.g., if(crmform.all.new_Researchid.DataValue == 'TEST');

    {

    var _Date=new Date(crmForm.all.<StartDate>.DataValue);
    var _Day=_Date.getDate();
    _Date.setDate(_Day+1);
    crmForm.all.<EndDate>.DataValue=_Date;

    }


    Apps
    Monday, December 20, 2010 3:23 PM
  • I am not sure if I understand your question clearly but if you want to check a specific value in lookup before this code you need to do like below

    if(crmform.all.new_Researchid.DataValue!=null)

    {

    if(crmform.all.new_Researchid.DataValue[0].name == 'TEST');

    {

    var _Date=new Date(crmForm.all.<StartDate>.DataValue);
    var _Day=_Date.getDate();
    _Date.setDate(_Day+1);
    crmForm.all.<EndDate>.DataValue=_Date;

    }

    }



    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 3:35 PM
    Moderator
  • got it. I'll use that. Thanks
    Apps
    Monday, December 20, 2010 3:39 PM
  • Hi, Mahender - I get 'Error:crmform is undefined' when I exit the Start Date field (I have put this code in the OnChange event of the Start Date field). Is that not correct?

    Based on whether the user chooses TEST in the Researchid lookup field and then populates the Start Date field, the End Date should show 2 days more than Start date.

    if(crmform.all.new_Researchid.DataValue!=null)

    {

    if(crmform.all.new_Researchid.DataValue[0].name == 'TEST');

    {

    var _Date=new Date(crmForm.all.new_startdate.DataValue);
    var _Day=_Date.getDate();
    _Date.setDate(_Day+1);
    crmForm.all.new_enddate.DataValue=_Date;

    }


    Apps
    Monday, December 20, 2010 4:33 PM
  • Hey, sorry may be I was in hurry here ...   :(  

    Please change like this

    if(crmForm.all.new_Researchid.DataValue!=null)

    {

    if(crmForm.all.new_Researchid.DataValue[0].name == 'TEST');

    {

    var _Date=new Date(crmForm.all.new_startdate.DataValue);
    var _Day=_Date.getDate();
    _Date.setDate(_Day+1);
    crmForm.all.new_enddate.DataValue=_Date;

    }

    }



    Mahain : My Dynamics CRM Blog
    Monday, December 20, 2010 4:45 PM
    Moderator
  • That works. Really appreciate your prompt responses. Thanks, again
    Apps
    Monday, December 20, 2010 5:24 PM
  • Meant to verify something with you...should I put this code only on OnChange event of Start Date and/or Form's OnLoad Event?

    thanks


    Apps
    Monday, December 20, 2010 5:27 PM