web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to see if a date field is greater or less than another date field....

(0) ShareShare
ReportReport
Posted on by 26

I need the logic below to show certain drop down options when Effective Start Date is > Original Effective Date and then to show different drop down option if Effective Start Date is < Original Effective Date.  My Script below isn't working and when I debug it, it doesn't tell me the error.

function YOYonChange()
{
var OriginalEffectiveDate = Xrm.Page.getAttribute("nhs_originaleffectivedate").getText();
var EffectiveStartDate = Xrm.Page.getAttribute("nhs_effectivestartdate").getText();
var YOY = Xrm.Page.getAttribute("nhs_formularytransitionyoymethodology").getValue();
 if (EffectiveStartDate == null || EffectiveStartDate =='' || EffectiveStartDate =='undefined')
 {
  Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();
 } 
 else if (EffectiveStartDate > OriginalEffectiveDate)
 {
  Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130000, text: 'Group'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130001, text: 'New Client'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130004, text: 'Traditional'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130002, text: 'Other'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130003, text: 'TBD'});
 }
 else if (EffectiveStartDate < OriginalEffectiveDate)
 {
  Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130000, text: 'Group'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130001, text: 'New Client'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130002, text: 'Other'});
    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130003, text: 'TBD'});
 }

function YOYOnLoad()
{
YOYonChange()
}

Any help is appreciated.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi ,

    You can refer below link.

    community.dynamics.com/.../246489

  • ashlega Profile Picture
    34,477 on at

    Hi,

     why don't you use getValue?

    var OriginalEffectiveDate = Xrm.Page.getAttribute("nhs_originaleffectivedate").getValue();

    var EffectiveStartDate = Xrm.Page.getAttribute("nhs_effectivestartdate").getValue();

  • Suggested answer
    Preeti Sharma Profile Picture
    2,678 on at

    Hi,

    You should compare values rather than text.Use following script code to get value

    var OriginalEffectiveDate = Xrm.Page.getAttribute("nhs_originaleffectivedate").getValue();

    var EffectiveStartDate = Xrm.Page.getAttribute("nhs_effectivestartdate").getValue();

    Now you can compare "OriginalEffectiveDate"  and "EffectiveStartDate".

    Check below link for more info:

    community.dynamics.com/.../crm-2011-comparing-only-dates-in-javascript

    Hope this helps:)

  • Suggested answer
    Arpit Shrivastava Profile Picture
    7,518 User Group Leader on at

    Hi,

    Please see my article for the same. Hope it helps

    arpitmscrmhunt.blogspot.in/.../compare-two-date-field-values-in.html

    Mark this an anwser if it helps.

    Cheers

    Arpit

    arpitmscrmhunt.blogspot.in

  • USA80 Profile Picture
    26 on at

    This got me past the issue now, so below is what I have.  However, now when I select an option from the drop down of YOY it doesn't stay.  Do you see anything that I am doing wrong?

    function YOYonChange()

    {

    var OriginalEffectiveDate = Xrm.Page.getAttribute("nhs_originaleffectivedate").getValue();

    var EffectiveStartDate = Xrm.Page.getAttribute("nhs_effectivestartdate").getValue();

    var YOY = Xrm.Page.getAttribute("nhs_formularytransitionyoymethodology").getValue();

    if (EffectiveStartDate == null || EffectiveStartDate =='' || EffectiveStartDate =='undefined')

    {

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();

    }

    else if (EffectiveStartDate > OriginalEffectiveDate)

    {

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130000, text: 'Group'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130001, text: 'New Client'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130004, text: 'Traditional'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130002, text: 'Other'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130003, text: 'TBD'});

    }

    else if (EffectiveStartDate < OriginalEffectiveDate)

    {

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130000, text: 'Group'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130001, text: 'New Client'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130002, text: 'Other'});

     Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130003, text: 'TBD'});

    }

    }

    function YOYOnLoad()

    {

    YOYonChange()

    }

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     you are clearing the options on every change, and, then, re-adding them. That way, you are loosing the selection. You don't really have to do that on every change.. Maybe do it this way:

    var YOYCase = null;

    function YOYonChange()

    {

    var OriginalEffectiveDate = Xrm.Page.getAttribute("nhs_originaleffectivedate").getValue();

    var EffectiveStartDate = Xrm.Page.getAttribute("nhs_effectivestartdate").getValue();

    var YOY = Xrm.Page.getAttribute("nhs_formularytransitionyoymethodology").getValue();

    if (EffectiveStartDate == null || EffectiveStartDate =='' || EffectiveStartDate =='undefined')

    {

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();

    }

    else if (EffectiveStartDate > OriginalEffectiveDate  && YOYCase != 1)

    {

    YOYCase = 1;

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130000, text: 'Group'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130001, text: 'New Client'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130004, text: 'Traditional'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130002, text: 'Other'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130003, text: 'TBD'});

    }

    else if (EffectiveStartDate < OriginalEffectiveDate && YOYCase != 2)

    {

    YOYCase = 2;

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").clearOptions();

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130000, text: 'Group'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130001, text: 'New Client'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130002, text: 'Other'});

    Xrm.Page.getControl("nhs_formularytransitionyoymethodology").addOption({value: 127130003, text: 'TBD'});

    }

    }

    function YOYOnLoad()

    {

    YOYonChange()

    }

  • USA80 Profile Picture
    26 on at

    The YOY field is still not staying.  I go to the field, after I put in a date and then select an option from the YOY field, but it doesn't stay.

  • USA80 Profile Picture
    26 on at

    OK, this seems to be working, and I know why it was dropping the option I was selecting.  It was because I had an onchange event on that field.  I removed it and now it stays.  However, when I try to save my record it says in the bottom right corner "unsaved changes"  why would this happen?

  • USA80 Profile Picture
    26 on at

    All required fields are filled in and there is no onSave javascript running.  Just wanted to give more info on this issue.

  • USA80 Profile Picture
    26 on at

    I got it working.  I had to grab the text if there was something in the YOY field on load so it didn't blank it out.  Thanks for all the help.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans