<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Optimizing Scheduled Jobs for Unarchiving the Incident in Community Central forum</title>
    <link>https://www.servicenow.com/community/community-central-forum/optimizing-scheduled-jobs-for-unarchiving-the-incident/m-p/3277284#M3270</link>
    <description>&lt;P&gt;Optimizing Scheduled Jobs for Unarchiving the Incident Table in ar_incident: We have developed a script to bulk restore records from the ar_incident table to the incident table. This script needs to handle approximately 8 million records. How to&amp;nbsp; improve the performance and speed up the restoration process, what strategies or techniques can be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the script updated in out scheduled job&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Initialize Counters and Log Storage&lt;BR /&gt;var incidentRestored = 0;&lt;BR /&gt;var childRecordsRestored = 0;&lt;BR /&gt;var logData = "Incident Number, Restored Child Record Sys ID\n";&lt;BR /&gt;// Query for Archived Change Requests created on or before 2025-08-31&lt;BR /&gt;var gr = new GlideRecord("ar_incident");&lt;BR /&gt;gr.addEncodedQuery('closed_at&amp;gt;javascript&amp;amp;colon;gs.dateGenerate("2018-05-30","23:59:59")');&lt;BR /&gt;gr.setLimit(25000); // Set to 2000 records for testing&lt;BR /&gt;gr.query();&lt;BR /&gt;//gs.info("Starting logging of archived incident requests...");&lt;BR /&gt;while (gr.next()) {&lt;BR /&gt;// Restore the Main incident Request&lt;BR /&gt;var archiveLogGR = new GlideRecord("sys_archive_log");&lt;BR /&gt;archiveLogGR.addQuery("id", gr.sys_id);&lt;BR /&gt;archiveLogGR.addNotNullQuery("archive");&lt;BR /&gt;archiveLogGR.setLimit(1);&lt;BR /&gt;archiveLogGR.query();&lt;BR /&gt;if (archiveLogGR.next()) {&lt;BR /&gt;var archiveRestore = new GlideArchiveRestore();&lt;BR /&gt;archiveRestore.restore(archiveLogGR.sys_id);&lt;BR /&gt;archiveRestore.restoreRelated(archiveLogGR.sys_id);&lt;BR /&gt;incidentRestored++;&lt;BR /&gt;//gs.info("Change request number: " + gr.number);&lt;BR /&gt;// Log Related Records&lt;BR /&gt;var gr2 = new GlideRecord("sys_archive_related");&lt;BR /&gt;gr2.addQuery("archive_map", archiveLogGR.archive);&lt;BR /&gt;gr2.query();&lt;BR /&gt;while (gr2.next()) {&lt;BR /&gt;var gr3 = new GlideRecord("sys_archive_log");&lt;BR /&gt;gr3.addQuery("archive_run", archiveLogGR.archive_run);&lt;BR /&gt;gr3.query();&lt;BR /&gt;while (gr3.next()) {&lt;BR /&gt;if (gs.getXMLText(gr3.payload, "//" + gr2.element) == archiveLogGR.id) {&lt;BR /&gt;// Restore Child Record&lt;BR /&gt;archiveRestore.restore(gr3.sys_id);&lt;BR /&gt;childRecordsRestored++;&lt;BR /&gt;//gs.info(" Child record sys_id: " + gr3.sys_id);&lt;BR /&gt;logData += gr.number + "," + gr3.sys_id + "\n";&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;// Create and Save the CSV File in sys_attachment ONLY IF logData is NOT empty&lt;BR /&gt;if (logData != "Incident Number, Restored Child Record Sys ID\n") {&lt;BR /&gt;var attachment = new GlideSysAttachment();&lt;BR /&gt;var fileName = "restoration_log_" + new GlideDateTime().getInternalFormattedValue() + ".csv";&lt;BR /&gt;var attachmentId = attachment.write(null, fileName, "text/csv", logData);&lt;BR /&gt;// Get the sys_id of the attachment record&lt;BR /&gt;var attachmentGr = new GlideRecord("sys_attachment");&lt;BR /&gt;attachmentGr.get(attachmentId);&lt;BR /&gt;var attachmentSysId = attachmentGr.sys_id;&lt;BR /&gt;if (attachmentSysId) {&lt;BR /&gt;//gs.info("Log file saved (sys_id): " + attachmentSysId);&lt;BR /&gt;}&lt;BR /&gt;} else {&lt;BR /&gt;//gs.info("No archived incident or child records found before the specified date.");&lt;BR /&gt;}&lt;BR /&gt;// Final Log Message&lt;BR /&gt;//gs.info("Logging complete!");&lt;BR /&gt;//gs.info("Incident Processed: " + incidentRestored);&lt;BR /&gt;//gs.info("Child Records Restored: " + childRecordsRestored);&lt;/P&gt;</description>
    <pubDate>Fri, 30 May 2025 06:16:12 GMT</pubDate>
    <dc:creator>shirleyB</dc:creator>
    <dc:date>2025-05-30T06:16:12Z</dc:date>
    <item>
      <title>Optimizing Scheduled Jobs for Unarchiving the Incident</title>
      <link>https://www.servicenow.com/community/community-central-forum/optimizing-scheduled-jobs-for-unarchiving-the-incident/m-p/3277284#M3270</link>
      <description>&lt;P&gt;Optimizing Scheduled Jobs for Unarchiving the Incident Table in ar_incident: We have developed a script to bulk restore records from the ar_incident table to the incident table. This script needs to handle approximately 8 million records. How to&amp;nbsp; improve the performance and speed up the restoration process, what strategies or techniques can be done?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the script updated in out scheduled job&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Initialize Counters and Log Storage&lt;BR /&gt;var incidentRestored = 0;&lt;BR /&gt;var childRecordsRestored = 0;&lt;BR /&gt;var logData = "Incident Number, Restored Child Record Sys ID\n";&lt;BR /&gt;// Query for Archived Change Requests created on or before 2025-08-31&lt;BR /&gt;var gr = new GlideRecord("ar_incident");&lt;BR /&gt;gr.addEncodedQuery('closed_at&amp;gt;javascript&amp;amp;colon;gs.dateGenerate("2018-05-30","23:59:59")');&lt;BR /&gt;gr.setLimit(25000); // Set to 2000 records for testing&lt;BR /&gt;gr.query();&lt;BR /&gt;//gs.info("Starting logging of archived incident requests...");&lt;BR /&gt;while (gr.next()) {&lt;BR /&gt;// Restore the Main incident Request&lt;BR /&gt;var archiveLogGR = new GlideRecord("sys_archive_log");&lt;BR /&gt;archiveLogGR.addQuery("id", gr.sys_id);&lt;BR /&gt;archiveLogGR.addNotNullQuery("archive");&lt;BR /&gt;archiveLogGR.setLimit(1);&lt;BR /&gt;archiveLogGR.query();&lt;BR /&gt;if (archiveLogGR.next()) {&lt;BR /&gt;var archiveRestore = new GlideArchiveRestore();&lt;BR /&gt;archiveRestore.restore(archiveLogGR.sys_id);&lt;BR /&gt;archiveRestore.restoreRelated(archiveLogGR.sys_id);&lt;BR /&gt;incidentRestored++;&lt;BR /&gt;//gs.info("Change request number: " + gr.number);&lt;BR /&gt;// Log Related Records&lt;BR /&gt;var gr2 = new GlideRecord("sys_archive_related");&lt;BR /&gt;gr2.addQuery("archive_map", archiveLogGR.archive);&lt;BR /&gt;gr2.query();&lt;BR /&gt;while (gr2.next()) {&lt;BR /&gt;var gr3 = new GlideRecord("sys_archive_log");&lt;BR /&gt;gr3.addQuery("archive_run", archiveLogGR.archive_run);&lt;BR /&gt;gr3.query();&lt;BR /&gt;while (gr3.next()) {&lt;BR /&gt;if (gs.getXMLText(gr3.payload, "//" + gr2.element) == archiveLogGR.id) {&lt;BR /&gt;// Restore Child Record&lt;BR /&gt;archiveRestore.restore(gr3.sys_id);&lt;BR /&gt;childRecordsRestored++;&lt;BR /&gt;//gs.info(" Child record sys_id: " + gr3.sys_id);&lt;BR /&gt;logData += gr.number + "," + gr3.sys_id + "\n";&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;// Create and Save the CSV File in sys_attachment ONLY IF logData is NOT empty&lt;BR /&gt;if (logData != "Incident Number, Restored Child Record Sys ID\n") {&lt;BR /&gt;var attachment = new GlideSysAttachment();&lt;BR /&gt;var fileName = "restoration_log_" + new GlideDateTime().getInternalFormattedValue() + ".csv";&lt;BR /&gt;var attachmentId = attachment.write(null, fileName, "text/csv", logData);&lt;BR /&gt;// Get the sys_id of the attachment record&lt;BR /&gt;var attachmentGr = new GlideRecord("sys_attachment");&lt;BR /&gt;attachmentGr.get(attachmentId);&lt;BR /&gt;var attachmentSysId = attachmentGr.sys_id;&lt;BR /&gt;if (attachmentSysId) {&lt;BR /&gt;//gs.info("Log file saved (sys_id): " + attachmentSysId);&lt;BR /&gt;}&lt;BR /&gt;} else {&lt;BR /&gt;//gs.info("No archived incident or child records found before the specified date.");&lt;BR /&gt;}&lt;BR /&gt;// Final Log Message&lt;BR /&gt;//gs.info("Logging complete!");&lt;BR /&gt;//gs.info("Incident Processed: " + incidentRestored);&lt;BR /&gt;//gs.info("Child Records Restored: " + childRecordsRestored);&lt;/P&gt;</description>
      <pubDate>Fri, 30 May 2025 06:16:12 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/optimizing-scheduled-jobs-for-unarchiving-the-incident/m-p/3277284#M3270</guid>
      <dc:creator>shirleyB</dc:creator>
      <dc:date>2025-05-30T06:16:12Z</dc:date>
    </item>
  </channel>
</rss>

