Script to get the month and year from the "created" field

carlh
Kilo Guru

Hi All, I'm looking for a way to create a more simple report showing incidents over time.  

The report I want to make requires me to be able to create a pivot table or bar chart where I can group by month and year

 

The report will conditions will be as follows.

Table = Incident

Column  - Custom field "u_month"

Row - Club Name

Created ON Last 12 months

Coul anyone help me with a script to put on the business rule on insert?

 

 

I've attached a screen shot of the hopeful result.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Carl,



here is your script which you can add to business rule and accordingly populate the month and year values in whichever field you want:



var nowDateTime = new GlideDateTime(current.sys_created_on);



var date = nowDateTime.getDate();



var month = date.toString().split("-")[1];


var year = date.toString().split("-")[0];



current.u_month = month;


current.u_year = year;



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

View solution in original post

11 REPLIES 11

Hi @Ankur ,

 

i tried the After, Insert+ Update BR on Incident and pasted your code. In the report i have selected only Month field. Because i need Report based on Months only. But in the report it's not showing the result as expected. Attaching screenshot of BR & Report.

VartikaGoel_0-1684496568712.png

VartikaGoel_1-1684496592568.png

 

 



Hi @Ankur Bawiskar ,

 

I need a report based based on month. I have created same BR with your code and created a field called months and added months as choice in that. But i am not getting the expected result. Attaching the BR and report.
BR- after- insert  + update 

VartikaGoel_0-1684497433281.png

Report- it is showing record with empty not with months

VartikaGoel_1-1684497526121.png