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

Remove the " " in the argument.  


No luck



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


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



==> 1: //var a = current.arrival.getDisplayValue();


2: //var b = current.departure.getDisplayValue();


3:


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


Hi,



This is the best I can help. I hope this will work for you.



  var arrival = new GlideDateTime(current.getDisplayValue('end_date')); //change the field label


  gs.info("Arrival: "+arrival);


  var departure = new GlideDateTime(current.getDisplayValue('start_date')); //change the field label


  gs.info("Dept: "+departure);


  var dur = GlideDateTime.subtract(departure, arrival);   // returns a glideduration object.


  gs.info(dur.getDisplayValue()); // This is your duration


  var gr = new GlideRecord('x_53167_hotel1_reservation');


  gr.addQuery('sys_id',current.sys_id);


  gr.query();


  if(gr.next()){


  gr.reservation.setDisplayValue('duration',dur); // I've assumed duration is "duration" type field.


  gr.update();


  }




Darshak


Shishir Srivast
Mega Sage

Please check if this works.



var a = new GlideDateTime("current.arrival.getDisplayValue()"); // do we need to pass parameter in quotes here?


var b = new GlideDateTime("current.departure.getDisplayValue()"); // do we need to pass parameter in quotes here?


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



var gr = new GlideRecord('x_53167_hotel1_reservation');


gr.get(current.sys_id');


gr.duration = c; //Hoping duration is not a reference field


gr.update();


This seems to work but in duration filed I get following. How to get it in for example 30 days


1970-01-09 00:00:00