Create or Update a Schedule Entry from Flow designer

axellfg
Tera Expert

I have to create some schedule entries on the 'cmn_schedule_span' from catalog item. I am achieving it through flow designer. I have a problem with timezones and I am not sure of how to configure them.

 

When I pass the value of the Date/Time variable, although I see my local time, the stored value is UTC time.

I am trying to convert this UTC to the logged user Time zone, but it is not working as expected. See an example:

 

I pass '2024-09-20 10:00:00', so the stored value it is '2024-09-20 08:00:00'.

When I pass this date to the 'Start date time' field on the schedule entry, the stored value is '20240920T080000', as it is an Schedule Date/Time. And the display value is '2024-09-20 08:00:00', but I want to see the date I have passed.

 

I have tried to set the time zone with:

var tz = gs.getSession().getTimeZone();
var gdt = new GlideDateTime(); 
gdt.setTZ(tz);

 But it didn't work. It returned '2024-09-20 08:00:00'. I have also tried:

var gdt = new GlideDateTime();
var tzo = gdt.getTZOffset()
gdt.add(tzo);

But it returned '2024-09-20 01:00:00'. Same result as when I try getDisplayValue() or getLocalTime().

What can I do to pass to the Schedule Entry the time on my Time Zone?

1 ACCEPTED SOLUTION

axellfg
Tera Expert

I solved it!!

The flow was running as System User, so the timezone from the Session was 'America/Los Angeles'. To take into the account the TimeZone from the User making the request, I have done:

var when = new GlideDateTime(subflow_inputs.when);

var grUser = new GlideRecord('sys_user');
if (grUser.get(subflow_inputs.user_id)) {
    var zoneName = grUser.time_zone;
    var tz = Packages.java.util.TimeZone.getTimeZone(zoneName);
    var gdt = new GlideDateTime();
    gdt.setTZ(tz);
    var offsetMillis = gdt.getTZOffset();
} else {
    var offsetMillis = 0;
}

when.add(offsetMillis);

I don't know if it is the optimal solution but I have tried a lot of things, and this is the only that seems to be working. I hope this helps someone, and if anyone has a suggestion, it is welcomed!

View solution in original post

1 REPLY 1

axellfg
Tera Expert

I solved it!!

The flow was running as System User, so the timezone from the Session was 'America/Los Angeles'. To take into the account the TimeZone from the User making the request, I have done:

var when = new GlideDateTime(subflow_inputs.when);

var grUser = new GlideRecord('sys_user');
if (grUser.get(subflow_inputs.user_id)) {
    var zoneName = grUser.time_zone;
    var tz = Packages.java.util.TimeZone.getTimeZone(zoneName);
    var gdt = new GlideDateTime();
    gdt.setTZ(tz);
    var offsetMillis = gdt.getTZOffset();
} else {
    var offsetMillis = 0;
}

when.add(offsetMillis);

I don't know if it is the optimal solution but I have tried a lot of things, and this is the only that seems to be working. I hope this helps someone, and if anyone has a suggestion, it is welcomed!