Calculated Value on a date field

Karine_M
Tera Guru

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' ?

Karine_M_0-1732872057642.png

 

regards,

 

Karine

1 ACCEPTED SOLUTION

Ashish Parab
Mega Sage

@Karine_M ,

 

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 -

ashish_parab_0-1732874126360.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

 

 

View solution in original post

2 REPLIES 2

Ashish Parab
Mega Sage

@Karine_M ,

 

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 -

ashish_parab_0-1732874126360.png

 

Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.

 

Thanks and Regards,

Ashish

 

 

Juhi Poddar
Kilo Patron

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