
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 08:55 AM
Hello - Looking to calculate a Contract Days Left field in the contract application form.
What I have works for the most part, but I would like the output to show 0 or "Expired".
Instead it shows hours and minutes that simply do not apply.
Also, Any to only show days and NOT show hours and minutes?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-14-2022 01:31 PM
Then, by changing the business rule I proposed to instead run on "display" and change the condition to only run if the end date is not empty should do the trick.
Another small note, I sure hope that there isn't any requirements in regards to reporting on this field. For example how many contracts do we have that has an expiry days less than 30 days.
That would still lead to the same issue as explained before, the values are not saved on the record, the value is only calculated when the record is displayed. If reporting is needed then reporting by using data on the "End date" field would be required.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 09:45 AM
Hello,
Unfortunately, for the duration field type, if you want only days to show, it can't be limited to just that. The attribute normally used would be max_time= and then days, hours, etc. While you can use "days"...that's the max unit used, meaning the lower unit of measurements such as hours and seconds, are still valid and would show. If you wanted only hours and no days, then this is where it's beneficial such as max_time=hours. Then...days would NOT show.
It sounds like you may need to use a string field or some other field type, do the calculation, format it as needed, then set the value for this field.
Even if you could display only days, you still wanted additional customization around it showing Expired, and it wouldn't be able to do that either and further justifying the need for a different field type.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-13-2022 10:09 AM
Understood. So If I create a String Field, what would be the code to calculate? Also, Would I need to do it via Business rule?

- 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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-19-2022 02:43 PM
How do we get the value to display in the list view? It current works in the form view