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

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.

Hello @Bert_c1 ,

this script gives below result where Related Record is undefined. 

*** Script: Processing 225 Archive story records.
*** Script: restoring Archive story 793f2d4c87531d18a702326e0ebb3556.
*** Script: restoring RELATED RECORD Archive story undefined.

  Does this require any kind of Archive Related Records rule? If yes, then what should be Reference and Reference table in Archive Related Records Rules?

I don't know which archive table you are referring to, is it ar_rm_story? You haven't posted the script you are using. Seems there is some problem with a record in the archive table.

Yes, I am referring to ar_rm_story table. Below is my script and I am having an issue with Related Record.

 

var arc = new GlideRecord('ar_rm_story'); 
arc.addEncodedQuery("themeSTARTSWITHCyber - Quest");
arc.query();
gs.info("Processing " + arc.getRowCount() + " Archive story records.")
while (arc.next()) {
gs.info('restoring Archive story ' + arc.sys_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.query();
gs.info('restoring RELATED RECORD Archive story ' + sal.sys_id + '.');
 
if (sal.next())
new GlideArchiveRestore().restoreMainAndRelated(sal.sys_id);
}