Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2022 10:38 AM
Hi,
Yes, setting the value with a Business rule would be recommended.
A before insert/update business rule, that runs whenever End date changes AND End date is not empty.
In the script you could run something like this:
(function executeRule(current, previous /*null when async*/) {
var now = new GlideDateTime();
var nowDate = now.getDate();
now.setValue(nowDate + ' 00:00:00'); // force the time to be able to compare with a date field.
var endDate = current.getValue('ends');
var endDateTime = new GlideDateTime(endDate + ' 00:00:00');
var daysDuration = GlideDateTime.subtract(now, endDateTime);
current.setValue('u_days_until_expire2', daysDuration.getDayPart());
})(current, previous);