Conditional script in scheduled job

raj10
Kilo Contributor

Hi team,

I just wanted to schedule a job with the script.

fetch all the number, comments , state, and state changed time from the incidents.

could you please help with the script. (conditional script and Run script)

Thanks for your help in advance,

 

raj

 

1 ACCEPTED SOLUTION

Hello Raj,

You still using my Old version of code.

Please update your code.,i already try to create a scheduled job on my personal instance and it is giving right solution of it.

Latest code is.

var grinc=new GlideRecord('incident');
grinc.query();
while(grinc.next())
{ 
   var grmet=new GlideRecord('metric_instance');
   grmet.addQuery('id',grinc.sys_id);//updated here
   grmet.addQuery('field','incident_state');
   grmet.orderByDesc('sys_created_on');
   grmet.setLimit(1);
   grmet.query();
   if(grmet.next());
   gs.print('Number-'+grinc.number+',State-'+grmet.value.toString()+',State_Change-'+grmet.sys_created_on);
  
} 

It is the updated code,please use this one.so yhat you will geT

INCIDENT NUMBER,Its current state,and the latest state change time.

Please keep posted for more help.I try the solution,it is working fine

Regards

Yash Agrawal

 

View solution in original post

51 REPLIES 51

Hi Paul

 

My requirement is state updated time, not incident time.

 

and I have scheduled the report , where can i find report in servicenow? do we recieve an email?( i have updated my email id, not recieved any email for the same)

Please add the Metric Created by column to the report and you will get the exact results as the code that is being provided, using a no-code approach.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Yash Agrawal1
Tera Guru

Hello Raj,

Please write the below code.

To fetch all the numbers, comments, state, and the state changed the time from the incidents.


var grinc=new GlideRecord('incident');
grinc.query();
while(grinc.next())
{ 
   var grmet=new GlideRecord('metric_instance_list');
   grmet.addQuery('grmet.id',grinc.sys_id);
   grmet.addQuery('field','incident_state');
   grmet.orderByDesc('sys_created_on');
   grmet.setLimit(1);
   grmet.query();
   if(grmet.next());
   gs.print('Number-'+grinc.number+' State-'+grinc.state+' State Change-'+grmet.sys_created_on);
  
}

 

Get the below output in Background Script

*** Script: Number-INC0010002 State-2 State Change-2020-09-29 11:49:31

 

Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.

Regards

Yash.K.Agrawal

Hi Yash,

 

Thanks for your help.

 

getting below error

find_real_file.png

Hello Raj,

I wrote the table name with _list.

Please use the below code, it will work 100%.

var grinc=new GlideRecord('incident');
grinc.query();
while(grinc.next())
{ 
   var grmet=new GlideRecord('metric_instance');//Updated Here.
   grmet.addQuery('grmet.id',grinc.sys_id);
   grmet.addQuery('field','incident_state');
   grmet.orderByDesc('sys_created_on');
   grmet.setLimit(1);
   grmet.query();
   if(grmet.next());
   gs.print('Number-'+grinc.number+',State-'+grinc.state+',State Change-'+grmet.sys_created_on);
  
}

 

Please Mark it helpful/correct if my answer helps in any way to resolve your query.
Reach out to me if any more help required.

Regards

Yash.K.Agrawal