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

Paramahanns
Tera Expert

Hi Vivek,



Can you please try and see if it helps.


var start = new GlideDateTime("2016-08-28 09:00:00");


var end = new GlideDateTime("2016-08-31 08:10:00");  


// Duration in hours  


var dur_seconds = gs.dateDiff(start, end, true); // returns the number of seconds as String  


var dur_hours = Math.round(dur_seconds / 3600); // Math.round() is optional and will round the number to the nearest integer  


gs.print(dur_hours);



I ran this in my background scripts and I get the result.



Regards


Param


Thanks. I am able to calculate the duration but not able to set its value in duration field as per the code above.  



Rama Chandra D
Kilo Guru

Hi Vivek,



In your script, gr is just a glide record object, so gr.setDisplayValue() doesn't know which record to update. So, if you want to update the current record, do a glide record query for sys_id on the table equal to 'current', and then update the record returned by query to set the field. If duration is a custom field, I wonder if it should have been, gr.setDisplayValue('u_duration',c).



var gr = new GlideRecord('x_53167_hotel1_reservation');


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


gr.query();


if(gr.next()) {


gr.setDisplayValue('duration',c); //check the field label here


gr.update();


}


Darshak


thank you for the good explanation:


Before business rule


Condition - (current.arrival.changes())



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');


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


gr.query();




if(gr.next()){


gr.reservation.setDisplayValue('duration',c);


gr.update();


gs.log(a);


}



Error:



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


Caused by error in sys_ui_action.42da42d00a0a0b340066377beb6dd099.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()");