Need to set the default value of days filed

maheshch18
Tera Contributor

Hi team,

 

We have one field "Expire Days" need to set the default value based on the difference between the current date and the end date.

 

For example:

Current Date is: 2023-08-18

End date: 2023-08-30

 

We need to set the value Expire Days as 12 days

 

Regards,

Mahesh

1 REPLY 1

Sagar Pagar
Tera Patron

Hi @maheshch18,

 

You need to write before insert/update business rule to calculate the difference between current date and end date.

 

Sample scripts:

var today = new GlideDateTime(); // gives current date-time
var end_date_time = new GlideDateTime('current.u_end_date'); // add end date field/column

var diff = GlideDateTime.subtract(today, end_date_time);
var days = diff.getRoundedDayPart();

current.expiry_days_field = days;

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow