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

Hello Paul,

If you read the comment properly you can see,i said that background script is simply a example to run ,try and check the code.

And the provided code is of Scheduled job,which is giving me the correct result,but not to Raj.

@raj- will you please provide input in this,that is your query resolved or not?

Regards

Yash Agrawal

@Yash Agrawal 

still checking, as logs are not appearing. let you know once it is done

@Paul Morris

need to get state changed time, not incident updated time.

i think using scheduled reports, we can get last updated time.

where we can see logs/data of scheduled reports?

Hello @raj

Will you please Create the new Schedued job once again,with the below code.

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

}

find_real_file.png

You can see the exact code.

And then Save the form and CLick on the "Execute Now" Button.

-----------------------------------------

After That go to System logs>>Warning

And in Messege colounm type "*Number".

100% of your query will get resolved.

Please try it once.

Regards

Yash Agarwal

Hi Yash,

 

thanks for your help.

results are not getting properly.

Incidents Details in ServiceNow :

find_real_file.png

logs in ServiceNow :

find_real_file.png

Hello Raj,

Please tell me,that did you change the field name in "metric_instance" table.

Please keep posted.

Regards

Yash Agrawal