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

The SN Nerd
Giga Sage
Giga Sage

Does Scheduled Reports not meet this requirement?


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

Hi Paul,

I am new to SNOW, exploring the knowledge myself so am not aware of scheduled reports.

and there are no scheduled reports in my developer Instance.

thanks and regards,

Raj

A key mantra in ServiceNow is not to code anything that can be done via configuration. In this case, it doesn't make sense to create a scheduled job for this requirement, especially if you are new to ServiceNow.

Create a Report in Report Designer then Schedule the report.

See steps below:

  • Data
    • Source type = table
    • Table = Incident Metric
  • Type
    • Type = List
  • Configure
    • Choose columns 'Number, Incident State, Definition, Value'
    • Filter: Definition = 'Incident State Duration'

Click Save.

find_real_file.png

Then, navigate to Sharing and select Schedule

find_real_file.png

Complete the form and schedule your report.

I don't see any Use Case for printing out data using Scheduled Scripts.


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

Hi Paul,

It was helpful.

but the result of state changed time is not matching.

 

Please find the below screenshots for your reference.

Incident Data

find_real_file.png

 

Report Data:

 

find_real_file.png

That just means that the record has been updated after the State was changed.

Not all updates show in the Activities log.


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