- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 02:32 AM
Hi,
We have a use case where the business requirement is to display a greeting on the service portal homepage that changes based on local time, and always displays the logged-in user's name. So far we have the widget's server script configured as such:
Server Script:
(function() {
var gdt = new GlideDateTime();
var dt = gdt.getLocalTime();
var t = dt.getByFormat('HH:mm:ss');
var split = t.split(":");
var hour = parseInt(split[0]);
var msg = '';
if (hour >= 0 && hour <= 11)
msg = "Good morning, ";
else if (hour >= 12 && hour <= 17)
msg = "Good afternoon, ";
else if (hour >= 18 && hour <= 23)
msg = "Good evening, ";
else
msg = "Good day, ";
data.greeting = msg;
data.title = options.title ? gs.getMessage(options.title) : gs.getMessage("");
data.short_description = options.short_description ? gs.getMessage(options.short_description) : false;
if (options.show_name != "true")
options.show_name = false;
else
options.show_name = true;
})();
We have it working like this:
However, at around 8-9am local timezone (GMT +8) or possibly at other times (we haven't been able to pinpoint the exact times it happens), the IF condition I have above stops working--it returns none of the 3 if/else if conditions above, which is why I set up the else{} condition for "Good day" as an interim workaround for that scenario.
Need help as I tried checking if it was syntax issue, I tried debugging each line of my parsing thru background script but it returns a number as expected and should properly work for the condition I setup above--did I miss anything?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 03:06 AM
Hi, I just tried your code down to the hour instantiation, and curiously while I get a result for user TZ settings of LA, London and HongKong, GMT returned NaN; and this may or maybe not related but it is a little bit odd.
You could try simplifying your code as javascript should understand 'string' versions of numbers, and I don't think you need to split the local time when you can use getByFormat('H') to do this for you. It may not fix your issue but perhaps try.
var gdt = new GlideDateTime();
var dt = gdt.getLocalTime();
var hour = dt.getByFormat('H');
gs.info('hour ' + hour);
Also I was recently working on some auth\login functionality and noticed that sometimes updated by fields were showing guest after user login, so possibly there is some session confusion (or lag) and adding some global server methods like getSession(), getSessionId() and getTimeZoneName() might help with debugging.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-07-2024 01:55 AM
Hi here is a code,
api.controller = function() {
/* widget controller */
var c = this;
var today = new Date();
var curHr = today.getHours();
if (curHr < 12) {
c.data.greeting = 'Good morning ' + scope.user.first_name+ ' ' + scope.user.last_name;
} else if (curHr < 18) {
c.data.greeting = 'Good afternoon ' + scope.user.first_name+ ' ' + scope.user.last_name;
} else {
c.data.greeting = 'Good evening ' + scope.user.first_name+ ' ' + scope.user.last_name;
}
};
find the solution if mark as helpful.