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

Community Alums
Not applicable

How does your post not have more thumbs up?! This is great stuff. 

 

Thanks for sharing. 

Hey,

 

Other than adding this into the client controller is there anything else we need to amend in the widget?

 

I've attached an XML of the widget for clarity. 

 

Kind Regards, 

 

Michael 

Richard Thomas2
Tera Contributor

That worked perfectly! Thank you so much Rishabh! šŸ™‚ šŸ™‚

Be aware that that script won't work correctly if the timezones are off, a user has a different timezone, etc..

That's why in my script, I'm using the LocalTime.

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

Richard Thomas2
Tera Contributor

Ah ok - thanks Mark will do šŸ™‚