Using a scheduled job to add an additional comment to a RITM

Erik Stuer
Tera Guru

I am trying to use a scheduled job to add an additional comment to RITM that meet a certain query. I want RITM that have the state open, approval requested, and created date more than 6 and less than 9 days from the query. Here is the code I came up with, but it isn't making any additional comments.

var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', 'open');
gr.query();

while (gr.next()) {
var comment = 'New comment here?';
gr.work_notes = comment + '\n\n' + gr.work_notes.getJournalEntry(1);
gr.update();
}

1 ACCEPTED SOLUTION

Erik Stuer
Tera Guru

Had a lot of issues with the scheduled job. One being testing was hard since it had to query soooo many records it actually slowed down the whole instance for a whole afternoon of testing. Ended up making it work with a flow. Trigger was when a request was made we did a wait for duration and then looked up the ritm table for those created relative to before 5 days ago and after 7 days ago then updated the record with the additional comment. 

View solution in original post

10 REPLIES 10

Thank you. This is mostly working. I tried replacing the work_notes with additional_comments as seen below but it won't make one now. Second issue is the post is being made by my user instead of the system. Do you know how to adjust it to have it show the system doing it?

 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', '1');
gr.query();
gs.info("Found " + gr.getRowCount() + " records to process.");
while (gr.next()) {
var newComment = 'Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval';
gr.additional_comments.setJournalEntry(newComment + '\n\n' + gr.additional_comments.getJournalEntry(1));
gs.info("Number: " + gr.number + " additional_comments = " + gr.additional_comments);
gr.update();
}

Hi Eric,

 

Your script failed for me, I updated mine with the new comment you have:

 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', '1');
gr.query();
gs.info("Found " + gr.getRowCount() + " records to process.");
while (gr.next()) {
//	var newComment = 'New comment here?';
	var newComment = 'Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval';
	var recentWN = gr.work_notes.getJournalEntry(1);
//	gs.info("work_notes.getJournalEntry = " + recentWN);
//	gs.info("Number: " + gr.number + " recentWN = " + recentWN);
	gr.work_notes.setJournalEntry(newComment + '\n\n' + recentWN);
	gs.info("Number: " + gr.number + " work_notes = " + gr.work_notes);
	gr.update();
}

 

and got the following output:

 

*** Script: Found 7 records to process.
*** Script: Number: RITM0010027 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:47 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:21 - System Administrator (Work notes)
New comment here?

2023-05-10 15:28:03 - System Administrator (Work notes)
Some work notes for REQ0010027






*** Script: Number: RITM0010002 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:48 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:22 - System Administrator (Work notes)
New comment here?






*** Script: Number: RITM0010026 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:48 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:22 - System Administrator (Work notes)
New comment here?

2023-05-10 15:28:42 - System Administrator (Work notes)
Some work notes for RITM0010026






*** Script: Number: RITM0010001 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:48 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:22 - System Administrator (Work notes)
New comment here?






*** Script: Number: RITM0000001 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:48 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:22 - System Administrator (Work notes)
New comment here?






*** Script: Number: RITM0010003 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:48 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:22 - System Administrator (Work notes)
New comment here?






*** Script: Number: RITM0010004 work_notes = Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-11 11:13:48 - System Administrator (Work notes)
Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval

2023-05-10 16:10:22 - System Administrator (Work notes)
New comment here?

where we can see the most recent comment was "New comment here?" from testing yesterday.

 

If you don't want to have your user as the source of the comment, then create a Scheduled Job and set the "Run as" to a suitable user (must meet ACLs);

I actually found the run as feature after I posted my last bit. So that should is good. I am trying to expand my query to RITM with the state of work in progress and pending as well. Those are the back end values of -5 and 2 for our instance. The below script is only making the updates on the open ones though. What is wrong about the below script? 

Also, I want the add a time feature to the query where it is only looking at the records created more than 6 days ago but less than 9 days ago. Any thought on how to do that?

 

 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', 'IN', ['1', '-5', '2']);
gr.query();
gs.info("Found " + gr.getRowCount() + " records to process.");
while (gr.next()) {
var newComment = 'Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval';
var recentWN = gr.work_notes.getJournalEntry(1);
gr.work_notes.setJournalEntry(newComment + '\n\n' + recentWN);
gs.info("Number: " + gr.number + " work_notes = " + gr.work_notes);
gr.update();
}

 

Also, I want these to be additional comments and not work notes. I tried just switch the work_notes to additional_comments but that didn't work.

Bert_c1
Kilo Patron

Hi Erik,

 

In this example I change 'work_notes' to 'comments', the desired field on the sc_req_item table.

 

var gr = new GlideRecord('sc_req_item');
gr.addQuery('state', '1');
gr.query();
gs.info("Found " + gr.getRowCount() + " records to process.");
while (gr.next()) {
//	var newComment = 'New comment here?';
	var newComment = 'Your manager has not approved this request. Please contact them to ensure the request for approval is being received. This request will be automatically declined after 30 days of submission for approval.';
	var recentWN = gr.comments.getJournalEntry(1);
//	gs.info("work_notes.getJournalEntry = " + recentWN);
//	gs.info("Number: " + gr.number + " recentWN = " + recentWN);
	gr.comments.setJournalEntry(newComment + '\n\n' + recentWN);
	gs.info("Number: " + gr.number + " comments = " + gr.comments);
	gr.setWorkflow(false);  // getting error on a business rule failing, so disabling.
	gr.update();
}

 

But I see when testing that I don't see the new comments activity on the record when I open it.  script debug shows the expected value, after adding setWorkflow(false). But that is not reflected when viewing the record. What I tested for work_notes field worked.