Calculate Age based on a choice field "Year" in hardware asset
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-06-2024 04:56 PM
Hi Snow Community,
I have a choice field in asset table called "Year" - choices are from (1990-current year). Now, I created a new field, 'u_age' with string type. I want to get the value of 'Age' by getting the today's date minus the 'Year' field.
For example, Year is 2000, Age will be 24 Years. I have tried creating a display BR but im not getting anywhere. Have search OOTB like for Incident, but the type used is duration. Below sample OOTB.
Display BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2024 10:00 PM
Just an update. I tried following a solution wherein, I created a script include and client script to calculate the value.
However, it displays NAN value. I'm stuck here. Anyone can help? Thank you.
Script include:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-08-2024 04:47 AM
// Get the current year
var currentYear = new GlideDateTime().getYearLocalTime();
// Parse the "Year" field to integer, assuming it's stored as a string
var assetYear = parseInt(current.getValue('Year'), 10);
if (!isNaN(assetYear) && assetYear <= currentYear) {
// Calculate the age
var age = currentYear - assetYear;
current.u_age = age + " Years";
} else {
// Handle any invalid or future year cases
current.u_age = "Invalid Year";
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-11-2024 11:09 PM
Hi nowitsvashu,
thanks for the response. Just wanted to confirm if this is on client script only that I need to update? or script include as well?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-12-2024 07:02 PM
Have tried you suggestion but still showing NAN/null value.