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

Dubz
Mega Sage

We calculate incident duration on our incident form using the below script in a before insert and update business rule that runs when the u_incident_fix field is changed.



current.u_incident_downtime= gs.dateDiff(current.opened_at.getDisplayValue(),current.u_incident_fix_time.getDisplayValue(),false);



i suppose for you it would be:



current.u_stay_duration=gs.dateDiff(current.arrival.getDisplayValue(),current.departure.getDisplayValue(),false);


What is the data type of opened_at, fix_time and incident_downtime ?


The opened_at and u_incident_fix_time fields are date/time, u_incident_downtime is a duration field.