- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 01:24 AM
Hi all !
I have got two field date (startdate and enddate) and a field NbMonth (as integer).
I want to use a formula to auto calculate the enddate = startdate + NbMonth.
Do you know if a formula exist to calculate this or do I have to through by a calculation type 'Script' ?
regards,
Karine
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 01:56 AM
Instead of using a formula, you can use script as a calculation type.
Script -
(function calculatedFieldValue(current) {
var startDate = current.u_start_date;
var nbMonth = current.u_nbmonth;
if (startDate && nbMonth) {
var gdt = new GlideDateTime(startDate);
gdt.addMonths(nbMonth);
return gdt;
}
return '';
})(current);
Output -
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 01:56 AM
Instead of using a formula, you can use script as a calculation type.
Script -
(function calculatedFieldValue(current) {
var startDate = current.u_start_date;
var nbMonth = current.u_nbmonth;
if (startDate && nbMonth) {
var gdt = new GlideDateTime(startDate);
gdt.addMonths(nbMonth);
return gdt;
}
return '';
})(current);
Output -
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2024 03:52 AM
Hello @Karine_M
Please try this script:
(function executeRule(current, previous /*null when async*/) {
if (current.startdate && current.NbMonth) {
var startDate = new GlideDateTime(current.startdate);
var nbMonths = parseInt(current.NbMonth, 10);
// Add the months
startDate.addMonths(nbMonths);
// Set the end date
current.enddate = startDate.getValue(); // Store the calculated end date
}
})(current, previous);
Hope this helps!
"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"
Thank You
Juhi Poddar