- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2020 02:11 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2020 03:17 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2020 05:27 AM
Hello Raj,
I will strongly suggest you, try the code on Background Script.
so that you can get the result understanding.
I want actually states(In Progress) instead of state number as 2 then.
gs.print('Number-'+grinc.number+',State-'+grmet.value.toString()+',State_Change-'+grmet.sys_created_on);
Regards
Yash Agrawal
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2020 05:41 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2020 05:50 AM
Hello Raj,
The background script is something, where you can try and test you code.
So to perform that ServiceNow provides you, a module called as "Background Script".
Please try and run the provided code. Here first, so that you can get the resulting understanding here.
Regards
Yash Agrawal

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2020 05:54 AM
Hello Raj,
Please check the updated code.
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);
}
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-29-2020 06:04 AM