Change greeting in Service Portal depending on the time

Richard Thomas2
Tera Contributor

Hi all

Currently in our portal we have this personalised greeting when users log in... we used this code 

function() {


/* widget controller */


var c = this;


c.data.greeting = 'Hi ' + scope.user.first_name + ',' + ' welcome to your Rathbones IT Service Portal';


}

find_real_file.png

Question: is it possible change the greeting depending on the time - so if it's pre 12 it would say 'Good morning Richard'

Thanks for you help

Richard

1 ACCEPTED SOLUTION

Rishabh Jha
Mega Guru

 

Hi @Richard Thomas ,

Since you're generating the greeting message on your widget's client controller, you'd need to use the javascript's date function, unless you want to make a sever side call to get the date-time using the Glide APIs. The javascript Date() will get the date-time from the user's pc.

Here's the snippet to do that:

function() {


/* widget controller */


var c = this;

var today = new Date();
var hour = today.getHours();

var greeting = '';
if(hour > 20 && hour <= 4) {
    greeting = greeting  + '${Good Night}';
} else if(hour > 4 && hour <= 12) {
    greeting = greeting  + '${Good Morning}';
} else if(hour > 12 && hour <= 16) {
    greeting = greeting  + '${Good Afternoon}';
} else if(hour > 16 && hour <= 20) {
    greeting = greeting  + '${Good Evening}';
}


c.data.greeting = greeting + ',  ' + scope.user.first_name + '!' + ' Welcome to your Rathbones IT Service Portal';


}

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

View solution in original post

11 REPLIES 11

Yes, Mark is right, and I've pointed that out in my answer that the date-time will be picked by the user's pc. So, if a user has gone to say, Australia for a month, and when he opens the portal in is laptop, he'd see the greeting according to the Australian timezone (if his pc is set to automatically change time-zone according to location), which I think is actually a good thing, like your smartphone or other devices show the time according to your current location.

In order to use the Glide APIs that Mark has mentioned, you'd need to make a server call from your client controller, and get the greeting evaluated accordingly from server side date-time, and in the above example, if your user goes to Australia for a month, but his TZ in ServiceNow is set to say Pacific, he'd see a Good Night greeting, when he opens the portal, even when it's morning time at his current location.

So depending on what your use-case is, and what suits your business needs best, you can go for either the client side or the server side solution.

 

Thanks & Regards,

Rishabh Jha

Aavenir (https://www.aavenir.com/)

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Did this solve your question? Or do we need to follow-up on this?

Please mark this answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn