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,

current object won't work in the condition field of scheduled job

what is the exact requirement?

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Just quickly set up some code, tested, works:

(function() {

	var getAllIncidents = new GlideRecord('incident');
	getAllIncidents.setLimit(5);
	getAllIncidents._query();

	while(getAllIncidents._next()) {
		var numberStr = getAllIncidents.getValue('number');
		var stateStr = getAllIncidents.state.getDisplayValue();
		var lastcommentStr = getAllIncidents.comments.getJournalEntry(1);
		var time = getAllIncidents.sys_updated_on.getDisplayValue();
		
		gs.info('>>> DEBUG: ' + numberStr + ' (' + stateStr + ' / ' + time + '): ' + lastcommentStr);
	}

})();

Can you describe a bit what you are actually after? Because you are using a condition, why? You are quering the whole incident table, why?

Though anyway, the script I provided works.

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,

 

Thanks a lot for your quick help.

really sorry if am struggling you. don't know am getting old script result(below screenshot) even after deleting the script and created a new script.

 

Conditions: 1. want to get all comments whenever an incident is inserted/updated

2. want to fetch state updated time whenever the incident state is changed

 

raj

find_real_file.png

So why did you choose a Scheduled Job. What you are describing know, sounds like this should be done with a Business Rule or Flow, which runs on update of a incident?

And only getting the information from that specific incident? I mean, if you would have thousands of incidents... you are querying all of them now.

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