Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by 2,645

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

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: How to see if a date field is greater or less than another date field....

    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.

  • USA80 Profile Picture
    USA80 2,645 on at
    RE: How to see if a date field is greater or less than another date field....

    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
    USA80 2,645 on at
    RE: How to see if a date field is greater or less than another date field....

    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
    USA80 2,645 on at
    RE: How to see if a date field is greater or less than another date field....

    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.

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to see if a date field is greater or less than another date field....

    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
    USA80 2,645 on at
    RE: How to see if a date field is greater or less than another date field....

    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()

    }

  • Suggested answer
    Arpit Shrivastava Profile Picture
    Arpit Shrivastava 7,518 User Group Leader on at
    RE: How to see if a date field is greater or less than another date field....

    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

  • Suggested answer
    Preeti Sharma Profile Picture
    Preeti Sharma 2,678 on at
    RE: How to see if a date field is greater or less than another date field....

    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:)

  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: How to see if a date field is greater or less than another date field....

    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
    gdas Profile Picture
    gdas 50,085 on at
    RE: How to see if a date field is greater or less than another date field....

    Hi ,

    You can refer below link.

    community.dynamics.com/.../246489

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans