Bulk Update RITM Closed Date

Sam Giles
Tera Contributor

Hi,

I'm looking for a hand writing a script to bulk update the 'closed' field in a number of RITMs. The date added should match the date of the last update in the request (This is the bit I'm stuck on). They are all in a closed state but the closed date was not populated. Any help would be appreciated.

Thanks,

1 ACCEPTED SOLUTION

Sruthi17
Kilo Sage

Hi,

Please try the below code.

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("stateIN3,4,7");
ritm.query();
while (ritm.next()) {
    ritm.closed_at = ritm.request.sys_updated_on; 
    ritm.update();
}
 
Please Accept as solution or mark helpful, if the information is useful to you.

View solution in original post

4 REPLIES 4

Sruthi17
Kilo Sage

Hi,

Please try the below code.

var ritm = new GlideRecord('sc_req_item');
ritm.addEncodedQuery("stateIN3,4,7");
ritm.query();
while (ritm.next()) {
    ritm.closed_at = ritm.request.sys_updated_on; 
    ritm.update();
}
 
Please Accept as solution or mark helpful, if the information is useful to you.

Harish Bainsla
Tera Sage
Tera Sage

(function() {
// Define the target table (Requested Item)
var ritmTableName = 'sc_req_item'; // Replace with the actual table name

// Get a GlideRecord for the Requested Item table
var ritmGR = new GlideRecord(ritmTableName);
ritmGR.addQuery('active', false); // Select closed records
ritmGR.query();

// Loop through closed Requested Items
while (ritmGR.next()) {
// Get the associated request
var request = ritmGR.request;
if (request && request.get('sys_updated_on')) {
// Get the last update date of the associated request
var lastUpdate = request.get('sys_updated_on');

// Set the 'closed' field in the RITM to match the last update date
ritmGR.closed = lastUpdate;
ritmGR.update();
}
}
})();

OlaN
Giga Sage
Giga Sage

Hi,

This sounds like something you could solve with a Flow also, no coding required.

Depending on the requirements, and the volume of records to update, this could be an option for you.

Have a look at the example I've provided below.

flow-update-records-one-time.png

Rohit52
Tera Contributor

how can we update the close at time of ritm with last sctask closed at time in servicenow. COuld youplease help me with solution here using background script