Date filters - Get records with a date in current month (regardless of the year)

sutot
Tera Contributor

Dears,

I need to get records (CIs) with Installed date in the current month, regardless of the year.

In ServiceNow there are already some 'trend' filter to do this, but only a static month can be used:

find_real_file.png

How can I implement the same filter, but with the condition 'Installed trend on current month'?

 

Thank you,

Sutot

 

 

1 ACCEPTED SOLUTION

hammad ul aziz2
Mega Guru

hi,

I got your issue you are looking for records installed on a current month regardless of the year.

So one thing that you can do is to create a script include and set the filter to sys_id isOneOf javascript:getTrendONCurrentMonth('field_name','table_name')

here is how you can use that script include in filter

find_real_file.png

script include should be client callable

find_real_file.png

here is the code for script include

this code will automatically get the current month and will return the trend on records.

function getTrendONCurrentMonth (fieldName,tableName) {

	var StringMonth='';
	var shortMonth='';
	var integerMonth=new GlideDateTime().getMonth();
	
	switch(integerMonth)
		{
				case 1:
					StringMonth='January';
					shortMonth='jan';
				break;
				case 2:
					StringMonth='February';
					shortMonth='feb';
				break;
				case 3:
					StringMonth='March';
					shortMonth='mar';
				break;
				case 4:
					StringMonth='April';
					shortMonth='apr';
				break;
				case 5:
					StringMonth='May';
					shortMonth='may';
				break;
				case 6:
					StringMonth='June';
					shortMonth='june';
				break;
				case 7:
					StringMonth='July';
					shortMonth='july';
				break;
				case 8:
					StringMonth='August';
					shortMonth='aug';
				break;
				case 9:
					StringMonth='September';
					shortMonth='sep';
				break;
				case 10:
					StringMonth='October';
					shortMonth='oct';
				break;
				case 11:
					StringMonth='November';
					shortMonth='noc';
				break;
				case 12:
					StringMonth='December';
					shortMonth='dec';
				break;
		}
	
	var sys_ids='';
	var g=new GlideRecord(tableName);
	g.addEncodedQuery(fieldName+"DATEPART"+StringMonth+"@javascript:gs.datePart('month','"+shortMonth+"','EE')");
	g.query();
	while(g.next())
	{
		if(sys_ids=='')
		{
			sys_ids+=g.getUniqueValue();
		}else{
			sys_ids+=','+g.getUniqueValue();
		}

	}
	return sys_ids.toString();
}

 

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Hammad ul Aziz

 

View solution in original post

9 REPLIES 9

asifnoor
Kilo Patron

Hi,

 

Instead of trend, why don't you use on operator and select "This month"

 

sutot
Tera Contributor

Dear asifnoor,

thank you for your answer.

'This month' condition is for the current month of the current year (e.g: July 2020). What I need is to get records with Installed date in this month, regardless of the year (e.g: July 2020, July 2019, July 2018, ... July 2015, ...)

Sutot

Aha. So you want records created in the month of July of any year?

If thats the case, then select

Trend on July and run.

Then it will filter all records of the month of july of any year.

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

Ankur Bawiskar
Tera Patron

Hi,

what if you give installed on this month filter?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader