- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 02:27 AM
Hello,
Need help. I do have one script that got problem. I need to get the day of week and return which day, so below is my script.
The condition in my script is it needs to get the day of tomorrow instead of today
Remark:
Today is 09/19/23 (Tues)
Tomorrow is 09/20/23 (Wed)
Script.
var gd = new GlideDate();
gd.addDaysLocalTime(1);
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(1);
var dayOfWeek = gdt.getDayOfWeek();
var whichday = {};
if (dayOfWeek == 1){
whichday = "Monday";}
else if (dayOfWeek == 2){
whichday = "Tuesday";}
else if (dayOfWeek == 4){
whichday = "Thursday";}
else if (dayOfWeek == 7){
whichday = "Sunday";}
gs.print (dayOfWeek);
gs.print(whichday);
I am not sure why it return as [object Object] instead of Wednesday,
Result
*** Script: 3
*** Script: [object Object]
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:04 AM
Hi @Bird1
Tomorrow will be Wednesday and i think you have not written if condition for Wednesday.
Highlighted in bold nad italics
var gd = new GlideDate();
gd.addDaysLocalTime(1);
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(1);
var dayOfWeek = gdt.getDayOfWeek();
var whichday = {
"day":'',
};
if (dayOfWeek == 1){
whichday.day= "Monday";}
else if (dayOfWeek == 2){
whichday.day = "Tuesday";}
else if (dayOfWeek == 3){
whichday.day = "Wednesday";}
else if (dayOfWeek == 4){
whichday.day = "Thursday";}
else if (dayOfWeek == 7){
whichday.day= "Sunday";}
gs.print (dayOfWeek);
gs.print(whichday.day);
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 02:45 AM
Hi @Bird1
Actually you are adding value in JSON object directly.It should be key:value pair.
Highlighted in bold
You can try below code :
var gd = new GlideDate();
gd.addDaysLocalTime(1);
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(1);
var dayOfWeek = gdt.getDayOfWeek();
var whichday = {
"day":'',
};
if (dayOfWeek == 1){
whichday.day= "Monday";}
else if (dayOfWeek == 2){
whichday.day = "Tuesday";}
else if (dayOfWeek == 4){
whichday.day = "Thursday";}
else if (dayOfWeek == 7){
whichday.day= "Sunday";}
gs.print (dayOfWeek);
gs.print(whichday.day);
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 04:59 AM
Test in background script, got empty value.
*** Script: 3
*** Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:04 AM
Hi @Bird1
Tomorrow will be Wednesday and i think you have not written if condition for Wednesday.
Highlighted in bold nad italics
var gd = new GlideDate();
gd.addDaysLocalTime(1);
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(1);
var dayOfWeek = gdt.getDayOfWeek();
var whichday = {
"day":'',
};
if (dayOfWeek == 1){
whichday.day= "Monday";}
else if (dayOfWeek == 2){
whichday.day = "Tuesday";}
else if (dayOfWeek == 3){
whichday.day = "Wednesday";}
else if (dayOfWeek == 4){
whichday.day = "Thursday";}
else if (dayOfWeek == 7){
whichday.day= "Sunday";}
gs.print (dayOfWeek);
gs.print(whichday.day);
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-19-2023 05:14 AM
Got it, thanks.