How to calculate / set a date/time field (Planned end date) based on another date/time AND duration type field?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2018 09:27 AM
Hi all, Re our CHG form I'm looking for assistance on how I can set/calculate the 'Planned end date' (type: date/time) field from the values entered on a 'Planned start date' (type: date/time) field and a new/custom 'Duration' (type: duration) field. I.e. user enters the Planned start date, enters how long the change is to take and then the Planned end date is automatically calculated / set.
While I have been able to successfully set the 'Duration' value based off on 'Planned start date' and 'Planned end date' values using the 'Calculated Value' attribute on the 'Duration' field, I have not been able to set 'Planned end date' based on 'Planned start date' and 'Duration'.
Many thanks in advance!
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2018 08:20 AM
Hi,
Write Before insert/update business rule. Refer following code.
Code:
(function executeRule(current, previous /*null when async*/) {
var start = new GlideDateTime(current.target_start);
var end = new GlideDateTime(current.target_finish);
var duration = GlideDateTime.subtract(start,end);
current.u_duration.setDateNumericValue(duration.getNumericValue());
})(current, previous);
Thanks,
Bhojraj
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2018 05:24 AM
Hi Raj, thanks for your input but again from what I can tell this is a business rule to set a 'Duration' type value.
I actually want to set/calculate a date/time value (for CHG field 'Planned end date') using the values from:
Planned start date (a date/time type field)
AND
Duration (a duration type field)
TO CALCULATE / SET:
Planned end date (a date/time type field)
Also ideally this will be client based so it updates on change of either first two fields as opposed to after a form save (business rule). Thanks for any further help in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2018 07:16 AM
UPDATE: I have been able to calculate/set the 'Planned end date' date/time field based on 'Duration' (type 'duration') field and 'Planned start date' date/time field using a business rule and the 'DurationCalculator' script include (progress of sorts at least).
I am also able to calculate/set 'Duration' based on changes to the 'Planned start date' and 'Planned end date' via client scripts calling the 'AjaxDurCalc' script include.
HOWEVER - ideally I would like to be able to set any of these fields via onChange client script(s) so any help on this is still much appreciated. In an ideal scenario a change to any of the 3 fields would result in an update to another field i.e.
IF...
Planned start date is modified by user
-If 'Planned end date' exists, set 'Duration'
-Else if 'Duration' exists, set 'Planned end date'
Duration is modified by user
-If 'Planned start date' exists, set 'Planned end date'
-Else if 'Planned end date' exists, set 'Planned start date'
Planned end date is modified by user
-If 'Planned start date' exists, set 'Duration'
-Else if 'Duration' exists, set 'Planned start date'