Calculate days left

Chris Richie
Tera Contributor

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?

find_real_file.png 

find_real_file.png

 

1 ACCEPTED SOLUTION

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.

View solution in original post

8 REPLIES 8

Allen Andreas
Administrator
Administrator

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!

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?

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);

 

How do we get the value to display in the list view? It current works in the form view

latroyamitchell_0-1671489789852.png