Script to show last month

Andrew_TND
Mega Sage
Mega Sage

I am not a Service now developer so please forgive my lack of understanding.

I'm looking to update a scheduled report subject with -  javascript:"This report is   for " +month_year_call()

The script for this to work is below, 

Where do I put this script in Service Now for this to function right?

function month_year_call() {

 

  var monthNames = ',December,January,February,March,April,May,June,July,August,September,October,November'.split(',');

 

var gdt = new GlideDateTime();

 

var month=(monthNames[new GlideDate().getMonthLocalTime()]+":"+gdt.getYear());

 

return month;

 

}


1 ACCEPTED SOLUTION

Mark Stanger
Giga Sage

Andrew,

I just responded to your other question with a solution for this.  I'll paste the solution here as well.  Take a look and let me know how it works.  If it solves your question, please mark the correct answer in both posts.

There's not a simple shortcut unfortunately (the method SN has just gives you a month number), but it can be done with your own script include function to produce the month name.  Here are the steps...

1)  Navigate to 'System Definition -> Script Includes' and create a new record with the following settings...

Name: u_dateUtils

Client callable: True

Script:

var u_dateUtils = Class.create();
u_dateUtils.prototype = {
	initialize: function() {

	},
	
	getMonthYearName :function(){
		var gdt = new GlideDateTime();
		var year = gdt.getYearLocalTime();
		var month = gdt.getMonthLocalTime();
		var monthName = '';
		if (month == 0)
			monthName = 'December';
		else if (month == 1)
			monthName = 'January';
		else if (month == 2)
			monthName = 'February';
		else if (month == 3)
			monthName = 'March';
		else if (month == 4)
			monthName = 'April';
		else if (month == 5)
			monthName = 'May';
		else if (month == 6)
			monthName = 'June';
		else if (month == 7)
			monthName = 'July';
		else if (month == 8)
			monthName = 'August';
		else if (month == 9)
			monthName = 'September';
		else if (month == 10)
			monthName = 'October';
		else if (month == 11)
			monthName = 'November';
		
		return monthName + ' ' + year;		
	},
	
	type: 'u_dateUtils'
};

2)  Set the 'Subject' field of your scheduled report with the following line.  It will call your custom function and return the Month/Year text you're looking for.  Adjust as needed.

javascript: 'Report created: ' + new u_dateUtils().getMonthYearName();

View solution in original post

6 REPLIES 6

Mark Stanger
Giga Sage

Andrew,

I just responded to your other question with a solution for this.  I'll paste the solution here as well.  Take a look and let me know how it works.  If it solves your question, please mark the correct answer in both posts.

There's not a simple shortcut unfortunately (the method SN has just gives you a month number), but it can be done with your own script include function to produce the month name.  Here are the steps...

1)  Navigate to 'System Definition -> Script Includes' and create a new record with the following settings...

Name: u_dateUtils

Client callable: True

Script:

var u_dateUtils = Class.create();
u_dateUtils.prototype = {
	initialize: function() {

	},
	
	getMonthYearName :function(){
		var gdt = new GlideDateTime();
		var year = gdt.getYearLocalTime();
		var month = gdt.getMonthLocalTime();
		var monthName = '';
		if (month == 0)
			monthName = 'December';
		else if (month == 1)
			monthName = 'January';
		else if (month == 2)
			monthName = 'February';
		else if (month == 3)
			monthName = 'March';
		else if (month == 4)
			monthName = 'April';
		else if (month == 5)
			monthName = 'May';
		else if (month == 6)
			monthName = 'June';
		else if (month == 7)
			monthName = 'July';
		else if (month == 8)
			monthName = 'August';
		else if (month == 9)
			monthName = 'September';
		else if (month == 10)
			monthName = 'October';
		else if (month == 11)
			monthName = 'November';
		
		return monthName + ' ' + year;		
	},
	
	type: 'u_dateUtils'
};

2)  Set the 'Subject' field of your scheduled report with the following line.  It will call your custom function and return the Month/Year text you're looking for.  Adjust as needed.

javascript: 'Report created: ' + new u_dateUtils().getMonthYearName();

That works brilliantly, how would I change the code in the subject to make it show last month?

Great!  If you want it to show last month you'll need to adjust that in the Script Include function by adding a single line directly below this line...

var month = gdt.getMonthLocalTime();

 

month = month - 1;

Please mark my answer as correct if it's answered your question.

in schedule reports what should i place here