How to write if condition on the bases of time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 06:52 PM - edited ‎07-24-2024 07:32 PM
I want to write a condition where if time is 8 AM to 10AM then new incidents assignment group should be "B" instead of "A".
Please suggest.
Need server side script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 08:56 PM
Hi @VIKAS MISHRA
Try this
var gdt = new GlideDateTime();
var timeSection = gdt.toString().split(' ')[1]; //Gets the Time
hour = timeSection.split(':')[0];
// Check if the current time
if (hour >= 8 && hour < 10) {
current.assignment_group =gs.getProperty('sys_idofGroupA');
} else {
current.assignment_group = gs.getProperty('sys_idofGroupB');
}
Please mark this response as correct or helpful if it assisted you with your question.
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 09:38 PM - edited ‎07-24-2024 09:47 PM
In my system the current time is as below when i am running code gs.print(gs.nowDateTime()) in backgroud script
*** Script: 2024-07-25 11:36:19
And when i am writing below mentioned script in background script , its evertime printing "No" and never printing yes, please suggest.
I tried to print "hour" to chekc what value is coming in this variable found its showing 04, howvever i should print 11
var gdt = new GlideDateTime();
var timeSection = gdt.toString().split(' ')[1]; //Gets the Time
hour = timeSection.split(':')[0];
// Check if the current time
if (hour >= 8 && hour < 12) {
gs.print('yes');
} else {
gs.print('no');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 09:56 PM
what's your time zone on the Service now User profile because when you run
var gt = new GlideDateTime(); it will take that profile user time zone
so make your not checking you local time so that's why its printing no
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-24-2024 10:27 PM
please try below code depending on your local timezone,i took IST i belong IST timezone.
var time = new GlideDateTime();
gs.info('GMT time is->' + time);
var targetTimezone = 'IST'; // ensure you give correct timezone Abbreviation
var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('IST time is->' + time);
var timeSection = time.toString().split(' ')[1]; //Gets the Time
var hour = timeSection.split(':')[0];
gs.print(hour)
if (hour >= 8 && hour < 12) {
gs.print('yes');
} else {
gs.print('no');
}
Output:
Please mark this response as correct or helpful if it assisted you with your question.
Thanks,
BK