Setting value on a 'Duration' field

kunal16
Tera Expert
  • On the rm_story table, I have a integer field 'Total Story Effort' which gives the value in hours based on some calculations.
  • On the same table rm_story, there is a field called Planned effort, which is a 'Duration' field. I have to copy the value of Total Story Effort to Planned effort in this format: ::
  • How can I achieve this?
3 REPLIES 3

BenPhillipsSNC
Kilo Guru

Hi Kunal,



Is this something that you need to perform one time, as a patch? Or is this an action you want to happen every time a user updates the rm_story record?


If this is a one time thing, you could use a script and run it in scripts - background.



Ultimately your question is:



rm_story.total_story_effort [datatype: DateTime] —how to convert to—> rm_story.planned_effort [datatype: Duration]



So, how to convert a DateTime object to a Duration object. Refer to this wiki: GlideDateTime - ServiceNow Wiki



You could probably use some of the example scripts such as declaring a new GlideDuration object then passing in the GlideDateTime value.




3.25.3 Example


var gdt = new GlideDateTime("2011-08-31 08:00:00"); //Wednesday


var dur = new GlideDuration();    


var span = gdt.getSpanTime(1); //how much time since Monday 00:00:00


dur.setValue(span);


gs.print(dur.getDisplayValue());






edwin_munoz
Mega Guru

Hello Kunal,



This script should do it (I'm guessing you are doing this from client-side):




g_form.setValue('effort', convertHours(g_form.getValue("total_story_effort")));//Change for correct fieled name



function convertHours(totalHours) {


 


      var days = Math.floor(totalHours/24);


 


      var hours   = Math.floor(totalHours%24);


 


      var minutes = Math.floor((totalHours%24 - hours) * 60 );


 


      var seconds =Math.floor((((totalHours%24 - hours) * 60) - minutes ) * 60);


 


      return days + " " + hours+ ":" + ('0' + minutes).slice(-2) + ":" + ('0' + seconds).slice(-2);





}




EDIT: I didn't notice that you said the total story effort is an integer field, if its an integer then there is no need to calculate minutes nor seconds, since they will always be zero.



Thanks.