Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

How can I automatically add 5 years to a date field?

Posted on by Microsoft Employee

Hi all, 

Hope everyone is safe and sound. I am new to dynamics and wondered what would be the correct solution to make the system automatically fill in the date field (step 4 in the scenario below)

User scenario:

  1. There are 3 fields for the user to fill in:
    1. Field A - dropdown list
    2. Field B - start date of item's validity
    3. Field C - end date of item's validity

  2. User selects "Yes" from a dropdown list in the field A
  3. User is prompted to enter the start date for the items validity in the field B. For example 2021-07-02
  4. After the user enters the start date in the field B, the system automatically fills in the end date in the field C by adding 5 years to the date entered in field B. For example - 2026-07-02

Could someone please advice if we can write business rules or add a JavaScript to fulfil step 4? Perhaps someone has solved similar challenges?

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: How can I automatically add 5 years to a date field?

    Hi, 

    You can create a Calculated field first:

    6786.pastedimage1625469368507v1.png

    4061.pastedimage1625469456444v2.png

    For more details on creating Calculated field, you can refer to this official documentation:

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/define-calculated-fields?view=op-9-1

    And then you can create a Business rule to judge whether the Field A is Yes:

    5314.pastedimage1625469543646v3.png

    2043.pastedimage1625469572473v4.png

    For more details on creating business rules, please refer to this official documentation:

    https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/customize/create-business-rules-recommendations-apply-logic-form?view=op-9-1

    This is my result:

    8562.pastedimage1625470103027v5.png

    If this helped you, I'd appreciate it if you'd mark this as a Verified Answer, which may in turn help others as well.

    Best Regards,

    Frank Gong

  • Suggested answer
    Daniel Schneider Profile Picture
    Daniel Schneider 170 on at
    RE: How can I automatically add 5 years to a date field?

    Hi,

    here the code:

    var test = test || {};

    test.Customizing = {

       Load: function () {

           Xrm.Page.getAttribute("your-field-with-value").addOnChange(this.AddYear);

       },

       AddYear: function () {

           var fieldBValue = Xrm.Page.getAttribute("your-field-with-value").getValue();

           var fieldCValue = new Date(fieldBValue.getFullYear() + 5, fieldBValue.getMonth(), fieldBValue.getDate());

           Xrm.Page.getAttribute("your-field-to-update").setValue(fieldCValue);

       }

    };

    add it to js file. Register it on Form to OnLoad Event.

    Greetz

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: How can I automatically add 5 years to a date field?

    Hello,

    There are some solutions for your scenario:

    1. Write a Javascript

    2. Write a system workflow

    3. you can make field B a calculated field and add 5 years to it. Please see the below screenshot:

    0285.pastedimage1625182674346v1.png

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans