Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how do I archive 350 stories from archived table?

Dk nov
Tera Expert

Hello,

I want to restore 350 records from archived story table, and it doesn't have list UI action 'Restore Record and Related Records' to restore all together with its related record. 

Restore Record and Related Records - List UI action is only available for Archive Log table. 

What are the possible options to do that?

 

Thanks.

2 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

You can use a script to process the 350 records.  Similar to below:

 

// Find sys_choice records where not present in any record.
var arc = new GlideRecord('ar_kb_use');		// substitue name of the archive table
// arc.addQuery('field', 'value');
arc.query();
gs.info("Processing " + arc.getRowCount() + " records.")
while (arc.next()) {
	gs.info('restoring ' + arc.id + '.');

	// restore the record, along with related records
	var sal = new GlideRecord("sys_archive_log");
	sal.addQuery("id", arc.sys_id);
	sal.addNotNullQuery("archive");
	sal.setLimit(1);
	sal.query();

	if (sal.next())
		new GlideArchiveRestore().restoreMainAndRelated(sal.sys_id);
}

logic in the loop is taken from the UI Action named 'Restore Record and Related Records'.  Test, in scripts - background. you can roll-back if you don't get the desired results. All records in my ar_kb_use table are no gone.

View solution in original post

I moved gs.info after if statement in script and it worked.

 

 

Thank you. 

View solution in original post

8 REPLIES 8

Your script worked in my PDI, after removing the 

arc.addEncodedQuery("themeSTARTSWITHCyber - Quest");

line. I had two records archived, now they are back in the rm_story table.

I moved gs.info after if statement in script and it worked.

 

 

Thank you. 

The restoreMainAndRelated() / GlideArchive.restoreRecordAndRelated() APIs look in the archive log for any entries created by those related-record rules and if none exist, there’s nothing to restore and the ID you’re logging shows up as undefined. To have real related records included, you must define an Archive Related Records rule for each child table you care about: set Table to the child table (for example, your story’s child table), set Reference to the field on that child that points back to the main story (like story or parent), and set Reference table to the main table (your story table). When archiving runs with that configuration, it will move both the main story and its related child rows into their respective archive tables and create proper log links. After that, restoreMainAndRelated() will restore both the story and those related records. Note that this only helps for data archived after the rule is in place - older archives that didn’t include related records will still have nothing “related” to restore.


ɪꜰ ᴍʏ ᴀɴꜱᴡᴇʀ ʜᴀꜱ ʜᴇʟᴘᴇᴅ ᴡɪᴛʜ ʏᴏᴜʀ Qᴜᴇꜱᴛɪᴏɴ, ᴘʟᴇᴀꜱᴇ ᴍᴀʀᴋ ᴍʏ ᴀɴꜱᴡᴇʀ ᴀꜱ ᴛʜᴇ ᴀᴄᴄᴇᴘᴛᴇᴅ ꜱᴏʟᴜᴛɪᴏɴ ᴀɴᴅ ɢɪᴠᴇ ᴀ ᴛʜᴜᴍʙꜱ ᴜᴘ.




ʙᴇꜱᴛ ʀᴇɢᴀʀᴅꜱ


ꜱʀᴇᴇʀᴀᴍ

Sreeram Nair
Tera Guru

Each archived table (e.g. story_A) contains the archived records themselves, but the Archive Log (sys_archive_log) table holds metadata about when and how those records were archived.
The “Restore Record and Related Records” UI action references that log entry to restore both the record and any related records, which is why it only appears on the Archive Log table, not the archive tables themselves.

 

Each time records are archived, ServiceNow creates an entry in the sys_archive_log table that tracks which records were moved and when. To restore your 350 archived story records, you can navigate to System Archiving > Archive Logs and filter the list by Table Name = story and the appropriate Archive Date. Once you locate the relevant archive log entries, select them all and use the “Restore Record and Related Records” list UI action. This action restores both the main Story records and their related records - such as attachments, comments, or tasks in a single operation.

If all 350 stories were archived in the same job, you can restore them together quickly and safely using this method, which is the cleanest and most ServiceNow-supported approach.


ɪꜰ ᴍʏ ᴀɴꜱᴡᴇʀ ʜᴀꜱ ʜᴇʟᴘᴇᴅ ᴡɪᴛʜ ʏᴏᴜʀ Qᴜᴇꜱᴛɪᴏɴ, ᴘʟᴇᴀꜱᴇ ᴍᴀʀᴋ ᴍʏ ᴀɴꜱᴡᴇʀ ᴀꜱ ᴛʜᴇ ᴀᴄᴄᴇᴘᴛᴇᴅ ꜱᴏʟᴜᴛɪᴏɴ ᴀɴᴅ ɢɪᴠᴇ ᴀ ᴛʜᴜᴍʙꜱ ᴜᴘ.




ʙᴇꜱᴛ ʀᴇɢᴀʀᴅꜱ


ꜱʀᴇᴇʀᴀᴍ