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

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Can you share what you've tried so far? Can you share where you are stuck? Can you share where exactly you need help with?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

am new to ServiceNow .

i tried, the result is not coming, please find the below screenshot

find_real_file.png

 

Just looking quickly:

find_real_file.png

while(record.next()){

This should at least be matching test. Can you update this?

Also add a gs.info statement at the start of your Run this script. Then you know if the script actually get executed at all, or that your condition does not match.

Concerning your condition... this is a Scheduled Job right? So what is current actually? So is your condition actually valid?

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi Mark,

it is really helpful.

I have changed the record to test and tried. getting below result which is not related to that script at all.

can you please help how can I write the condition. if I want to fetch the values

 

Thanks for your help

 

raj

find_real_file.png