- 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
06-19-2023 02:46 AM
Hello @ariannegrocales
except below script everything looks good please check below script that options is having values in it.
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;
Please mark this response as correct or helpful if it assisted you with your question.
Regards,
Nitesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2023 02:55 AM
Hi Nitesh,
The script you mentioned has no bearing on the Greeting part with the issue: data.title has no value, data.short_description is only the small wordings beneath, and options.show_name is to display the name of the logged in user.
As for the greeting, I made it display only the result of the scripted condition 'msg' only, so no other value should be showing up.
For the HTML portion, it's displaying as such:
<h1 class="text-center text-4x m-b-lg sp-tagline-color"><span ng-if="options.show_name" class="tagline-name">{{data.greeting}} {{::user.first_name}}!</span> {{::data.title}}</h1>
- 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
06-26-2023 06:46 PM
Hi Tony,
Thanks for the reply, the simplification helped with my issue. 😊
Trying the above sample out on a background script at around 9am GMT+8 returns me with a proper 9, whereas my original script returns me as NaN still. I'll adjust my condition to the method above, thanks again!