Duration Field Values

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2019 07:21 AM
Hi,
From looking around at the various forums it's clear that people struggle with setting values in duration fields. Serveral time's I've been able to get info messages to show the right calculated value but not been able to get it to show correctly in the duration field.
I'd thought I'd share a script that worked by converting the numeric value (getNumericValue) to hours, performing the calculation and the converting the result back to milliseconds in order to populate the duration field.
One of the fields I wanted to use was a string containing a manually entered number of hours and I wanted a calculated duration field to be substracted from that and the result put into a third duration field. Adding an info message to display the calc variable will show you a very long number because:
1 ms = 2.7777777777778E-7 hours
But don't worry about that because the duration field interprets this and displays it in hours (presuming you have told it to show hours).
(function executeRule(current, previous /*null when async*/) {
var plan = current.getValue('u_planned_effort_hours');
var seconds = (current.work_effort.getGlideObject().getNumericValue()*0.001/3600); //gets value in milliseconds and converts to hours
var actual = current.getDisplayValue('work_effort');
var remain = (plan - seconds); //calculates difference in hours
var calc = remain/0.001*3600; //converts result back to milliseconds
current.remaining_effort.setDateNumericValue(calc); //populates relevant field
current.update();
return '';
// Add your code here
})(current, previous);
This is an after business rule because one of the values is calculated already and running is as before meant it kept taking the previous value rather than the current value.
I hope this makes sense. I'm happy to take corrections or suggestions if what I have done can be done in a better way.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2019 08:38 AM
DON'T USE current.update ON BUSINESS RULES.....D'oh!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-05-2019 05:21 AM
Actually, I had current.update() on a BR that duplicated field values from a parent project onto child projects.
I didn't write it and, in practise, it did the recursive action thing and told users they were trying to add a record thjat already existed. So I removed the current.update() from the script and it stopped working entirely.
I came across this post which says to use:
current.setWorkflow(false);
current.update();
I have no idea why or what it actually does but it works and tests have confirmed that it doesn't start recursive activities.
I was so surprised by this after reading multiple posts saying, point blank, don't use it, that I decided to post an addendum to my previous post.