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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

We are using below code on our Virtual Agent. Instead of the return, use your c.data.greeting. And instead of vaInputs.user.first_name use your scope.user.first_name. I think this could work already.

var greetingStr = gs.getMessage('G\'day');
var hourInt = new GlideDateTime().getLocalTime().toString().split(' ')[1].split(':')[0];

if(hourInt < 7) {
    greetingStr = gs.getMessage('Good night');
} else if(hourInt >= 7 && hourInt < 12) {
    greetingStr = gs.getMessage('Good morning');
} else if(hourInt >= 12 && hourInt < 18) {
    greetingStr = gs.getMessage('Good afternoon');
} else if(hourInt >= 18) {
    greetingStr = gs.getMessage('Good evening');
}
	
return greetingStr + ' ' + vaInputs.user.first_name + '!';

If my answer helped you in any way, please then mark it as helpful.

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

That's fantastic - Thank you Mark - I will try this now šŸ™‚ 

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/)

A minor correction on the first if condition is needed, should be an || -


if(hour > 20 || hour <= 4) {

 

Thanks & Regards,

Rishabh Jha

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