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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Divya,

you can use timer activity

sharing links for help

https://docs.servicenow.com/bundle/orlando-servicenow-platform/page/administer/workflow-activities/r...

https://community.servicenow.com/community?id=community_question&sys_id=385643e1db1cdbc01dcaf3231f96...

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Gowrisankar Sat
Tera Guru

Hi,

You need to calculate the difference from current date time to end time and set it in timer. Use subtract function for this.

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server/no-namespace/GlideCalendarDat...

Thanks for your reply. For getting the time difference, I need both current date time & end_time in the same time zones.

So can you help me with getting the current date time in any specified time zone?