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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Is there a way to calculate age from a Date of Birth field?

(0) ShareShare
ReportReport
Posted on by

I am currently using CRM 2013 and need to calculate age.  I only have DOB to go by.  I need to send an email when the contact is approaching a specific age.  Can this be done via workflow or business rule?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Tim Dutcher Profile Picture
    2,100 on at

    Search the forum for the following: birthday email send

    You'll find lots of responses/tips on this.

  • Verified answer
    Kumar Viju Profile Picture
    1,026 on at

    Hi,

    You need to write Custom Code to meet your requirement. 

    https://community.dynamics.com/crm/f/117/p/214885/570073#570073 

  • Verified answer
    Iswarya Profile Picture
    1,347 on at

    Hi Ben,

    you can calculate age by using javascript, i achieved this scenario in ms crm 2016 below my code

    function AgeCategory() {
    var age = 0;
    var Now = new Date(); //Todays Date
    var Birthday = Xrm.Page.getAttribute("new_Dateofbirth").getValue();

    //Get the Date of Birth value
    if (Birthday != null) {
    var diff = Now.getMonth() - Birthday.getMonth(); //Check to see if Birthday has already passed
    if (diff > -1) //If Birthday has already occurred
    {
    var bd1 = Now.getFullYear() - Birthday.getFullYear();
    age = bd1.toString();

    }
    else //If Birthday has not already occurred
    {
    var bd2 = Now.getFullYear() - Birthday.getFullYear() - 1;
    age = bd2.toString();

    }
    }
    }

  • Verified answer
    Karuna Karan Profile Picture
    1,212 on at

    JS to calculate Age of Contact

    1. Create custom field 'Age' in whole number format, and then put it in the form. Remember the schema name of this field + lock the field in form.

    2. Web Resource Type: Javascript

    function CalculateAge(birthday, ondate) {

     // if ondate is not specified consider today's date

     if (ondate == null) { ondate = new Date(); }

     // if the supplied date is before the birthday returns 0

     if (ondate < birthday) { return 0; }

     var age = ondate.getFullYear() - birthday.getFullYear();

     if (birthday.getMonth() > ondate.getMonth() || (birthday.getMonth() == ondate.getMonth() && birthday.getDate() > ondate.getDate())) { age--; }

     return age;

    }

    function birthdateOnChange() {

    // read the birthday value from the birthdate field

    var birthday = Xrm.Page.getAttribute("birthdate").getValue();

    // if the date contains a value calculate the age

    if (birthday != null) {

     // get the age

     var age = CalculateAge(birthday);

     // set the age to the new_age field

     Xrm.Page.getAttribute("new_age").setValue(age);

    }

    }

    Replace the 2 schema names in the code with yours.

    3. Add the Web Resource to Form Libraries, then to the Event Handlers and call function "birthdateOnChange"

    4. In the Form, double click the DOB field and in the event tab under Event Handlers, press add and then select library and call this function "birthdateOnChange".

    5. Save and publish the entity.

    6. Enter data or change data in DOB | if data already there then on form onload you will see the correct Age!

  • Suggested answer
    Vipin J Profile Picture
    1,603 on at

    The business rule where seems to be not working ang giving an error - You can't use Now(), which is of type dateTime, with the current function.

    So i wrote a JavaScript to support my calculation of Age. Click Here

    vjcity.blogspot.com/.../how-to-calculate-age-in-dynamic-365-crm.html

  • Namrata11 Profile Picture
    77 on at

    Please find the steps-

    1) Calculate the Age using Calculated field

    2) Copy the Age to a whole number using Java Script (calculated field's onchange event.)

    3) Write a workflow on the change of Whole number field (Having Timeout when the condition will satisfy)

  • stjohnt Profile Picture
    15 on at

    Using your code, when saving my changes to the entity, the age field clears and on load the form always displays "unsaved changes." Do you know what could be causing these issues?

  • Suggested answer
    A Verma Profile Picture
    55 on at

    You can also achieve this without writing any custom code via Power Automate (need to use an expression for calculating age). Please refer the below link:

    learningrefresh.home.blog/.../

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans