Convert days entered into hours

Andrew Bettcher
Kilo Sage

Hello,

I have a business rule the converts a duration field (Hours, Minutes and Seconds) into a number of working days represented (based on a 7.5 hour working day):

(function executeRule(current, previous /*null when async*/) {

	
	var time = (current.effort.dateNumericValue());
	current.u_planned_effort_days.setDisplayValue(time/(60*60*7.5*1000));
	
	// Add your code here

})(current, previous);

 

It works fine but the operators want to option to add planned effort to a project either by entering the number of hours (and having it converted and displayed) OR entering the number of days and having it converted to hours.

We need to keep the concept of hours because our workforce uses hours to record their time on time cards.

It seemed simple to reverse this BR  so that it looks like this:

(function executeRule(current, previous /*null when async*/) {

	
	var time = (current.u_planned_effort_days);
	current.effort.setDisplayValue(time*(7.5));
	
	// Add your code here

})(current, previous);

I tried it with .dateNumericValue in place first. Didn't work and so I removed it based n the idea that the value is already numeric. This calculation should simply take whatever number is added to u_planned_effort_days and the set the value of current.effort to whatever it is multipled by 7.5.

It doesn't work.

Any ideas?

 

10 REPLIES 10

Anurag Tripathi
Mega Patron
Mega Patron

Hey,

 

Why dont you just get the value of duration changed to hours instead of days?? You can do it using setting the max_unit attribute as explained in the link below

https://docs.servicenow.com/bundle/london-application-development/page/script/general-scripting/concept/c_SettingTheDurationFieldValue.html#d197154e174

 

Please mark my answer correct/helpful if it solves your issue.

-Anurag

-Anurag

Andrew Bettcher
Kilo Sage

That link is handy. Thank you.

 

I've more or less achieved what I want with this:

(function executeRule(current, previous /*null when async*/) {

	
	var time = (current.u_planned_effort_days.getDisplayValue());
	var effort = (time*(7.5));
	current.effort.setDateNumericValue(effort);
	gs.addInfoMessage(effort);
	current.effort.update();

	
	// Add your code here

})(current, previous);

The info message displays the correct value and, if I set the current.effort to populate a normal string field it also works. I just need to work out why the duration field isn't recognising the value I'm giving it.

Perhaps something to do with the fact that it needs milliseconds or perhaps it needs a defined format somewhere?

 

Anyone?

instead of 

current.effort.update();

you just need

current.update();

 

Please mark my answer correct/helpful if it helped you solve your issue.
-Anurag

-Anurag

Andrew Bettcher
Kilo Sage

Hi - That is an improvement to my script but it still doesn't work. The info message I added as a test shows the correct value in hours but it doesn't put it into the duration field. The duration field is just blank. I suspect it's because the duration field is actually holding 3 values somehow (Hours, Minutes and Seconds).

Other duration fields that I have the on the form with calculation/roll up values display as a string but this renders them read only which is no good for this particular field as users want the flexibility.