Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

(0) ShareShare
ReportReport
Posted on by

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
    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
    172 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
    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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans