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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

(0) ShareShare
ReportReport
Posted on by 72

Hi all,

I am attempting to create a contacts age in a custom calculated field of data type whole number.

So far I have:

1. written the following code.js:

function calculateAge(birthdate) {
var today = new Date();
var birthDate = new Date(birthdate);
var age = today.getFullYear() - birthDate.getFullYear();
var monthDiff = today.getMonth() - birthDate.getMonth();

if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}

return age;
}

2. Uploaded as a web resource

3. Added library to form that contains both fields (birthdate and age)

4. opened calculated field and in action i attemoted to put the following

           calculateAge(birthdate)

pastedimage1681206573244v1.png

but it says:

          calculateAge function does not exist.

I would reaaly appreciate any help or advice please.

Many thanks

Mathu

I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    Hello,

    Calculated fields do not support javascript functions.

    You can create a plugin or use Power Automate to perform this calculation.

  • Matthu Profile Picture
    72 on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    OK thanks for responding Xavier - I appreciate it.

    Is there a way to this javascript to run on a standard whole number field instead?

  • Verified answer
    Guido Preite Profile Picture
    54,084 Moderator on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    javascript runs on client-side, meaning the user need to open the form an trigger an event (like the form onload or a change of a field) in order to execute the javascript. If you need to calculate it server-side as Xarier Monin suggested, you can write a plugin.

    hope it helps

  • Matthu Profile Picture
    72 on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    Thank you Guido, That's really useful too.

    I don't have any experience in the process of writing a plugin sadly. I will go and read up on that.

    Thanks both for your help. If there are any good plugin documents you like please signpost me to them - if not then thanks again and have a great day :-)  

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    Technically, you can execute this javascript on client side to calculate the age.

    However, there will be undesirable effects:

    - This code can be executed to calculate the age when the user changes the date of birth. The age will become wrong, for example, if the user opens the contact one year later because the age is not recalculated.
    - It can be executed when the form is loaded, but the age will be wrong in the views. Also, it is bad practice to change data when the form is loaded. We could use the setSubmitMode("never") trick to not save the age in the database, but that doesn't solve the problem of incorrect age in the views and database.

    I recommend using Power Automate to do this calculation.

    Nevertheless, it would look like this with javascript (only for science):

    function setCalculatedAge(execContext) {
        const formCtx = execContext.getFormContext();
    
        const birthDateAttr = formCtx.getAttribute("birthdate"); // Change birthdate field name if needed
        const ageAttr = formCtx.getAttribute("age"); // Change age field name
    
        const birthDate = birthDateAttr.getValue();
    
        // Calculate age
    
        const today = new Date();
        let age = today.getFullYear() - birthDate.getFullYear();
        const monthDiff = today.getMonth() - birthDate.getMonth();
    
        if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
            age--;
        }
    
        if (ageAttr.getValue() !== age) {
            ageAttr.setValue(age);
        }
    }

    Register setCalculatedAge function to on change event on the birthdate field. Ensure to check the `Pass execution context as first parameter` checkbox.

  • Guido Preite Profile Picture
    54,084 Moderator on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    this is the official documentation page

    https://learn.microsoft.com/en-us/power-apps/developer/data-platform/tutorial-write-plug-in

    and on youtube you can find several videos, like this one:

    https://www.youtube.com/watch?v=FwctAEGf07o

  • Matthu Profile Picture
    72 on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    That's fantastic thankyou, I really appreciate the notes on good practice, It is hard to know which is the right approach - the javascript is fascinating. Thankyou for your help!

  • Matthu Profile Picture
    72 on at
    RE: JavaScript function doesn't exist error message? Trying to record contact age from D.O.B

    Brilliant Guido, Thankyou. I will read, watch and learn! Thankyou so much for your 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

Responsible AI policies

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

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 247

#2
Tom_Gioielli Profile Picture

Tom_Gioielli 166 Super User 2025 Season 2

#3
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 164

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans