Calculating Duration

vivektietkumar
Giga Contributor

I am trying to calculate the duration of stay on a table called reservation. I have arrival and departure fields on it. I wrote a before business rule  

var a = new GlideDateTime("current.arrival.getDisplayValue()");

var b = new GlideDateTime("current.departure.getDisplayValue()");

var c =GlideDateTime.subtract(a,b);

var gr = new GlideRecord('x_53167_hotel1_reservation');

// the code works fine till here.

gr.setDisplayValue('duration',c); // this line doesn't work fine.

gr.update();

I get the error that - Function setDisplayValue is not allowed in scope x_53167_hotel1

I tried changing to gr.reservation.setDisplayValue('duration',c) and then I get the following error:

org.mozilla.javascript.EcmaError: The undefined value has no properties.

Caused by error in sys_script.0fe98c150f444300890ecf8ce1050e9e.script at line 9

6: var c =GlideDateTime.subtract(a,b);

7:

8: var gr = new GlideRecord('x_53167_hotel1_reservation');

==> 9: gr.reservation.setDisplayValue('duration',c);

10: gr.update();

Thanks in advance.

17 REPLIES 17

Please try below if this helps.



var a = new GlideDateTime(current.arrival.getDisplayValue());


var b = new GlideDateTime(current.departure.getDisplayValue());


var c = gs.dateDiff(a, b, false);


var gr = new GlideRecord('x_53167_hotel1_reservation');


gr.get(current.sys_id');


gr.duration = c.split(' ')[0];


gr.update();


Paramahanns
Tera Expert

Hi Vivek,



What is the datatype of all the three fields that you have, Arrival,Departure, Duration. What is the difference you want to see as a result.



Regards


Param


Paramahanns
Tera Expert

I was trying this.



find_real_file.png



var gdt1 = new GlideDateTime(current.arrival.getDisplayValue());
var gdt2 = new GlideDateTime(current.departure.getDisplayValue());
//var dur = new GlideDuration();


var dur = GlideDateTime.subtract(gdt1, gdt2); //the difference between gdt1 and gdt2
current.duration=dur.getDisplayValue();



I was using this in the before business rule.



I am using the Arrival,Departure as Date/Time, Duration as String Duration.



or I think you can use the duration type itself. I have posted an example below you can tweak as you need.



Actual Start Date


field name: startdate


Type: Date/time



Actual End Date


field name: enddate


Type: Date/time



Duration Type


field name: duration


Type: Duration



Create a business rule with the below code on which ever needed. Like on update or insert.



getTimeDiff();  


    function getTimeDiff(){  
  var startDate = current.u_startdate.getGlideObject();  
  var endDate = current.u_enddate.getGlideObject();  
 
  current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);



find_real_file.png



Regards


Param


Tried the following code in a before business rule.


Condition - (current.arrival.changes())


Try1:



getTimeDiff();


    function getTimeDiff(){  


  var startDate = current.arrival.getGlideObject();  


  var endDate = current.departure.getGlideObject();  


 


  current.duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);


    }




error:


Function getGlideObject is not allowed in scope x_53167_hotel1 - Any idea on how to remediate this ?



Try 2: added reservation in


getTimeDiff();


    function getTimeDiff(){  


  var startDate = current.x_53167_hotel1_reservation.arrival.getGlideObject();  


  var endDate = current.x_53167_hotel1_reservation.departure.getGlideObject();  


 


  current.duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);


    }



Error:


org.mozilla.javascript.EcmaError: Cannot read property "arrival" from undefined


Caused by error in sys_ui_action.42da42d00a0a0b340066377beb6dd099.script at line 1



==> 1: getTimeDiff();


2: function getTimeDiff(){


3: var startDate = current.x_53167_hotel1_reservation.arrival.getGlideObject();


4: var endDate = current.x_53167_hotel1_reservation.departure.getGlideObject();



Thanks again!


Rama Chandra D
Kilo Guru

Do we have any update on this?



Darshak