Calculate Age using JavaScript in D365
Calculate Age using JavaScript in D365
To get Age Value Using JavaScript prerequisites are :
1. Create A Date Column (Field) in Table(Entity) and name it "Date of Birth" or "DOB"
Copy the Schema Name : Ex: new_dob ;
2. Create An Age Column(Field) in Table (Entity) and name it "Age"
Copy the Schema Name : Ex: new_age ;
3. Write A JavaScript Code in any Editor as below or directly in WebResource
function getAge(executionContext)
{
debugger // Used for debugging Errors
var formContext = executionContext.getFormContext();
var dob = formContext.getAttribute("new_dob").getValue(); //Getting value from dob field
var today = new Date(); // getting current date value and storing in "today" variable
var birthdate = new Date(dob) // passing dob variable into new Date and storing in birthdate variable
var age = today.getFullYear()-birthdate.getFullYear(); //getting Full Year Values from today & Birthdate
formContext.getAttribute("new_age").setValue(age); // Setting value to Age Field
}
4. Create A WebResource Library and Paste Code over there Save and Publish all Customizations
5. Trigger an onChange Event on "Date of Brith " Column(field)
6. Pass the Function Name getAge and Check box on Passing execution context at First Parameter, Click Done
7. Here we go Code starts working in your Application
For more Clarifications and Help ping me in Gmail: samsonkongala1@gmail.com
Comments
Post a Comment