Calculate 2 date fields to return an Integer (days)

caliban
Tera Expert

Hi there,

 

I'm sure this has been asked many times before so apologies, but any solutions I've tried for dates don't seem to work.

I have a start date and an end date for a conference, and I want to calculate these two fields to fill in a 'Number of Nights' field which is an integer, to assist with accommodation booking.

 

I have tried the following as a before business rule

  current.number_of_nights = gs.dateDiff(current.start_date.getDisplayValue(),current.end_date.getDisplayValue(),false);

But I get the error ' Function dateDiff is not allowed in scope sn_hbm. Use GlideDateTime.subtract() instead'

so if I try

(function executeRule(current, previous /*null when async*/) {

	var checkIn = new GlideDateTime(current.checkin_date);
	var checkOut = new GlideDateTime(current.checkout_date);
	var duration = GlideDateTime.subtract(checkIn,checkOut);
	return number_of_nights.getDisplayValue();

})(current, previous);

The business rule will run but the 'number_of_nights' field is still empty.

Any assistance will be greatly appreciated

1 ACCEPTED SOLUTION

In my code above I was just modifying the function you provided. If this is executing in a business rule and you want to update the number of nights, you'll have to set that field within the rule. So the business rule code would look something like this:

 

(function executeRule(current, previous) {
    var checkIn = new GlideDateTime(current.checkin_date);
    var checkOut = new GlideDateTime(current.checkout_date);

    // Assuming the field is named number_of_nights and is an integer field:
    current.number_of_nights =
            GlideDateTime.subtract(checkIn, checkOut).getDayPart();

    // If this is an after or async rule, be sure to update the record:
    // current.update(); // Leave commented out if this is a before rule
})(current, previous);

 

If your field name is something other than number_of_nights, just substitute the correct field name in instead.

View solution in original post

4 REPLIES 4

Dennis R
Tera Guru

The nutshell explanation is that GlideDateTime.subtract returns a GlideDuration object, not an integer. You're reeeealy close, though. Try this:

 

(function executeRule(current, previous ) {
    var checkIn = new GlideDateTime(current.checkin_date);
    var checkOut = new GlideDateTime(current.checkout_date);
    return GlideDateTime.subtract(checkIn,checkOut).getDayPart();
})(current, previous);

 

Hope this helps,

--Dennis R

 

Hi Dennis,

Thanks for your response, I tried your code as detailed and that still does not give me the number of days.

I understand that you are saying the GDT.subtract will not return an integer, but an object.  If that is the case do you have any suggestions on how to add the returned object into the 'Number of Nights' field so that it displays the correct calculation?

Many thanks for your assistance 

In my code above I was just modifying the function you provided. If this is executing in a business rule and you want to update the number of nights, you'll have to set that field within the rule. So the business rule code would look something like this:

 

(function executeRule(current, previous) {
    var checkIn = new GlideDateTime(current.checkin_date);
    var checkOut = new GlideDateTime(current.checkout_date);

    // Assuming the field is named number_of_nights and is an integer field:
    current.number_of_nights =
            GlideDateTime.subtract(checkIn, checkOut).getDayPart();

    // If this is an after or async rule, be sure to update the record:
    // current.update(); // Leave commented out if this is a before rule
})(current, previous);

 

If your field name is something other than number_of_nights, just substitute the correct field name in instead.

Mihir Mohanta
Kilo Sage

gs.dateDiff() function does not work in the scoped environment.

 

You can refer the below link and achieve this through script include.

 

https://community.servicenow.com/community?id=community_question&sys_id=0b62cbeddb98dbc01dcaf3231f961906&view_source=searchResult