Workflow wait for condition based on variable date/Time and Timezone

divyasurada
Tera Contributor

Hi,

I have a scoped workflow that needs to wait until specified "end_time" (date/Time) along with "timezone" on catalog item form is completed. 

I only know I need to use Timer activity for this can you please help me with the scripting? 

Thanks in advance.

1 ACCEPTED SOLUTION

Anirban Roy
Mega Guru

Hi Divya,

Adding a timezone might give a better user experience but it can/ should be avoided to avoid any developmental overheads. 

You may keep a single field (date time) and request the user to select the appropriate time considering his own timezone.

Also, the value of the variable on the RITM variable editor form would be confusing as well. For example, value during ticket creation by an IST user at 16:30 + CET timezone, would display 13:00 +CET for an european users. (reason is servicenow stores the datetime backend value converted to UTC based on the the ticket creators timezone while displays based on the current logged in users timezone) 

 

*********************************************************************************
However, in case you decide to go with the custom solution, you can follow the below approach using moment.js library

 

 

gs.include("moment.js");
gs.include("moment-timezone-with-data-1970-2030.js");

var ritmGr = new GlideRecord("sc_req_item");
ritmGr.get("ea6d8dd61b799c102f967669cd4bcb54");//sys_id of a RITM record


var tZSetOnVar='';
var shouldBeTZ = ritmGr.variables.u_timezone.toString();


//--calculate the timezone of the requestor
var usrGr = new GlideRecord('sys_user');
usrGr.addQuery('user_name',ritmGr.sys_created_by);
usrGr.query();
if(usrGr.next())
{
tZSetOnVar= usrGr.time_zone.toString();
}

if(typeof tZSetOnVar=='undefined' || tZSetOnVar=='')

{tZSetOnVar = gs.getProperty('glide.sys.default.tz');
}

var requestedDateTime = new GlideDateTime(ritmGr.variables.u_schedule_downtime_start);
var currentDateTime = new GlideDateTime();


var tZSetOnVarOffSet = moment.tz(moment.utc(), tZSetOnVar).utcOffset();
var shouldBeTZOffSet = moment.tz(moment.utc(), shouldBeTZ ).utcOffset();

var totalSpan = requestedDateTime.getNumericValue() - currentDateTime.getNumericValue() + tZSetOnVarOffSet*60*1000 - shouldBeTZOffSet*60*1000 ;// as the requestor might be in a different timezone than the requested for user, we need to apply the timezone offset between requestor's timezone and requested for's timezone

gs.info("Total timespan in millis: "+totalSpan);// this will give the actual time difference from current time to the actual downtime requested
	

 

Output:

find_real_file.png

find_real_file.png

 

 

Helpful link -

https://stackoverflow.com/questions/32878197/updating-time-offset-with-moment-utcoffset

 

moment.js library can be imported from -

https://momentjs.com/

 

 

Regards,

Anirban

View solution in original post

9 REPLIES 9

Hi Divya,

This is for current time:

var tz = "<Timezone value> i.e.Canada/Eastern";

var gDT = new GlideDateTime();

gDT.setTZ(tz);

var abc = gDT.getDisplayValue();

 

For a field time use:

var tz = "<Timezone value> i.e.Canada/Eastern";

var gDT = new GlideDateTime(current.<field_name>);

gDT.setTZ(tz);

var abc = gDT.getDisplayValue();

Check this link for reference: https://community.servicenow.com/community?id=community_question&sys_id=4714cfe5dbd8dbc01dcaf3231f96...

 

The mentioned methods are not working for scoped application

Function setTZ is not allowed in scope.
Use of Packages calls is not permitted in scoped applications.

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

On time you can also script and provide a schedule for that time activity.

See this:

find_real_file.png

 

So in answer you can push the current.variables.end_time


Thanks,
Ashutosh 

Anirban Roy
Mega Guru

Hi Divya,

Adding a timezone might give a better user experience but it can/ should be avoided to avoid any developmental overheads. 

You may keep a single field (date time) and request the user to select the appropriate time considering his own timezone.

Also, the value of the variable on the RITM variable editor form would be confusing as well. For example, value during ticket creation by an IST user at 16:30 + CET timezone, would display 13:00 +CET for an european users. (reason is servicenow stores the datetime backend value converted to UTC based on the the ticket creators timezone while displays based on the current logged in users timezone) 

 

*********************************************************************************
However, in case you decide to go with the custom solution, you can follow the below approach using moment.js library

 

 

gs.include("moment.js");
gs.include("moment-timezone-with-data-1970-2030.js");

var ritmGr = new GlideRecord("sc_req_item");
ritmGr.get("ea6d8dd61b799c102f967669cd4bcb54");//sys_id of a RITM record


var tZSetOnVar='';
var shouldBeTZ = ritmGr.variables.u_timezone.toString();


//--calculate the timezone of the requestor
var usrGr = new GlideRecord('sys_user');
usrGr.addQuery('user_name',ritmGr.sys_created_by);
usrGr.query();
if(usrGr.next())
{
tZSetOnVar= usrGr.time_zone.toString();
}

if(typeof tZSetOnVar=='undefined' || tZSetOnVar=='')

{tZSetOnVar = gs.getProperty('glide.sys.default.tz');
}

var requestedDateTime = new GlideDateTime(ritmGr.variables.u_schedule_downtime_start);
var currentDateTime = new GlideDateTime();


var tZSetOnVarOffSet = moment.tz(moment.utc(), tZSetOnVar).utcOffset();
var shouldBeTZOffSet = moment.tz(moment.utc(), shouldBeTZ ).utcOffset();

var totalSpan = requestedDateTime.getNumericValue() - currentDateTime.getNumericValue() + tZSetOnVarOffSet*60*1000 - shouldBeTZOffSet*60*1000 ;// as the requestor might be in a different timezone than the requested for user, we need to apply the timezone offset between requestor's timezone and requested for's timezone

gs.info("Total timespan in millis: "+totalSpan);// this will give the actual time difference from current time to the actual downtime requested
	

 

Output:

find_real_file.png

find_real_file.png

 

 

Helpful link -

https://stackoverflow.com/questions/32878197/updating-time-offset-with-moment-utcoffset

 

moment.js library can be imported from -

https://momentjs.com/

 

 

Regards,

Anirban