Scritpt for restoring archived data with related records

geogeorge
Tera Expert

Hi All,

 

Currently, I am using a code to restore records from the `ar_incident` table, but I am unable to restore the records along with their related lists. Please find the code below:

 

var aritm = new GlideRecord('ar_incident');
aritm.addEncodedQuery('assignment_groupLIKEDTSC^number=INC98766');
aritm.query();
while(aritm.next()){

var archiveLog = new GlideRecord('sys_archive_log');
archiveLog.addQuery('id', aritm.sys_id);
//archiveLog.addNullQuery('restored');
archiveLog.query();
if (archiveLog.next()) {
    var und = new GlideArchiveRestore().restore(archiveLog.sys_id);
    var und2 = new GlideArchiveRestore().restoreRelated(archiveLog.sys_id);
    if (!und) {
        gs.log("The RITM restore failed : "+aritm.number,'RITM Restore');
    } else {
         gs.log("RITM Restored : "+aritm.number,'RITM Restore');
    }
} else {
     gs.log("No active archive log entry found.  Record probably already restored : "+aritm.number, 'RITM Restore');
}
}

 

3 REPLIES 3

Hitoshi Ozawa
Giga Sage
Giga Sage

You also need to query the sys_archive_related table to restore related.

Check the code in the following thread.

https://www.servicenow.com/community/developer-forum/how-to-restore-multiple-archived-incidents/m-p/...

Aniket Chavan
Tera Sage
Tera Sage

Hello @geogeorge ,

Please give a try to the script below and see how it works for you!

var aritm = new GlideRecord('ar_incident');
aritm.addEncodedQuery('assignment_groupLIKEDTSC^number=INC98766');
aritm.query();

while (aritm.next()) {
    var archiveLog = new GlideRecord('sys_archive_log');
    archiveLog.addQuery('id', aritm.sys_id);
    archiveLog.query();

    if (archiveLog.next()) {
        // Restore the primary record
        var primaryRestore = new GlideArchiveRestore().restore(archiveLog.sys_id);

        // Log primary restore status
        if (!primaryRestore) {
            gs.log("Primary record restore failed for : " + aritm.number, 'Incident Restore');
        } else {
            gs.log("Primary record restored successfully for: " + aritm.number, 'Incident Restore');
        }

        // Restore related records
        var relatedArchive = new GlideRecord("sys_archive_related");
        relatedArchive.addQuery("archive_map", archiveLog.archive);
        relatedArchive.query();

        while (relatedArchive.next()) {
            var relatedRecord = new GlideRecord("sys_archive_log");
            relatedRecord.addQuery("archive_run", archiveLog.archive_run);
            relatedRecord.query();

            while (relatedRecord.next()) {
                var relatedRestore = new GlideArchiveRestore().restore(relatedRecord.sys_id);
                
                if (relatedRestore) {
                    gs.log("Related record restored successfully for: " + aritm.number, 'Incident Restore');
                } else {
                    gs.log("Failed to restore related record for: " + aritm.number, 'Incident Restore');
                }
            }
        }
    } else {
        gs.log("No active archive log entry found for: " + aritm.number + ". Record may already be restored.", 'Incident Restore');
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

Hello @geogeorge ,

I noticed you marked my answer as helpful! If you still have any doubts, feel free to reach out so we can address them. If everything is clear, please consider marking my answer as the accepted solution—this helps future readers and will allow us to close the thread.

Thank you!😉