GlideArchiveRecord usage

Sue Frost
Giga Guru

We originally set up archiving some time ago and recently realized that we've been accidentally orphaning a number of tables in a related record.

Case (gets archived) along with related Communication record but (custom) Attachment record related to Communication record is NOT archived.

I have a go-forward fix, the issue I'm running into is with clean-up.

I've built a temporary table and a script to store details on the records in Attachments that need to be archived. By creating an entry for each record in the temp table, I can ask ServiceNow to create the relationship between the Attachment and the Communication in the archive tables. All I need to do is accurately identify the records.

The problem I'm stuck at is that when I run this code, it identifies one record in Attachments (grA) and creates a record in the temp table (grN) and then proceeds to actually archive 410,001 records from the Attachments table. It looks like the command new GlideArchiveRecord().archive(grA); is actually attempting to archive the entire table until the job times out.

Has anyone worked with this command before? The only references I can find to it in are in my instance and the global UI Action "Archive Record" where it runs against 'current'. Is there something I can do to reference 'current' or absolutely ID the record I want to archive in the scheduled job. Complete script is below.

Thank you!

- - - - - - - - - -

archiveCleanup();

function archiveCleanup(){

  //gs.log('Into function', "SUF");

  try {

  //gs.log('Into try', "SUF4");

  var grC = new GlideRecord('ar_u_communications');

  grC.orderBy('created');

  grC.setLimit(10);

  grC.query();

  while (grC.next()) {

            var grA = new GlideRecord('u_attachments');

            grA.addQuery('task', grC.sys_id);

            grA.query();

            while (grA.next()) {                               //found a match

                      gs.log('Found a match for ' + grC.number, "SUF");

                      var grN = new GlideRecord('u_archive_correction');

                      grN.initialize();

                      grN.u_attachment_sys_id = grA.sys_id;

                      grN.u_file_name = grA.u_file_name;

                      grN.u_communication_number = grC.number;

                      grN.u_communcation_sys_id = grC.sys_id;

                      grN.insert();

                      var cNumber = grC.number;

                      gs.log('Got an comm record - ' + cNumber, "SUF4");

                      new GlideArchiveRecord().archive(grA);

  }

  }

}

catch(err) {

  gs.log("Error: " + err, "SUF");

}

gs.log('Finished', "SUF");

}

1 ACCEPTED SOLUTION

Sue Frost
Giga Guru

Replying back to close the loop on this question.



It appears that new GlideArchiveRecord().archive(grA) will archive the selected record - not the entire collection.


However, we ran into another issue where more records were being archived than were being tracked in the Archive Correction table. I feel like this is a logic flaw in my code but I could.not find the answer.



We'd given this task too much time already, so as much as I'd like to solve it, we're just going to live with it. It's better to have records 'orphaned' in the live table than in the archive. At least in the live table, they will be re-attached if the parent is restored.


View solution in original post

5 REPLIES 5

Sue Frost
Giga Guru

Replying back to close the loop on this question.



It appears that new GlideArchiveRecord().archive(grA) will archive the selected record - not the entire collection.


However, we ran into another issue where more records were being archived than were being tracked in the Archive Correction table. I feel like this is a logic flaw in my code but I could.not find the answer.



We'd given this task too much time already, so as much as I'd like to solve it, we're just going to live with it. It's better to have records 'orphaned' in the live table than in the archive. At least in the live table, they will be re-attached if the parent is restored.


Hi @Sue Frost , 

 

I'm trying to restore data from archived table. I'm using glidearchiverestore function but since my tables are in scoped application this function is not supported. Is there any other alternative?? 

 

Can we write this script in global scope and restore records in custom scope? 

We've written some script that runs both on a global and on a scoped application to restore records.

It runs off a service request where the user indicates the case numbers to be restored.

 

I've attached the code.

 

 
 
   Hii Thanks for Your suggestion , But i tried to do with this it closing child of that related record i don't want to be close the child when the it running on related table as well i just want to close only those child which matched the condition in related archiving rule.   
I create two rule on for incident and one for incident task .
on task i created two record which maching the condition and one in not matched the condition and attached with same incident which match my incident data archiving rule and it is closing both task but i want to close only one task which match the condition ..

i wrote script for as well
 /* var CustomGlideArchiver = Class.create();
CustomGlideArchiver.prototype = {
    initialize: function() {},
    archiveByRule: function(sysid) {
        var ar = new GlideRecord('sys_archive');
        ar.addQuery('active=true^sys_id=' + sysid);
        ar.query();
        if (ar.next()) {
            var gr = new GlideRecord(ar.table);
            gr.addEncodedQuery(ar.getValue('condition').toString());
            gr.query();
            while (gr.next()) {
                new GlideArchiveRecord().archive(gr);
                var arr = new GlideRecord('sys_archive_related');
                arr.addQuery('archive_map=' + ar.sys_id);
                arr.query();
                while (arr.next()) {
                    if (arr.table_archive_rule && arr.table_archive_rule.active == true) {
                        var ar1 = new GlideRecord('sys_archive');
                        ar1.addQuery('sys_id=' + arr.table_archive_rule);
                        ar1.query();
                        if (ar1.next()) {
                            var gr1 = new GlideRecord(ar1.table);
                            gr1.addEncodedQuery(ar1.getValue('condition').toString());
                            gr1.addQuery(arr.getValue('element').tostring(), gr.sys_id);
                            gr1.query();
                            while (gr1.next()) {
                                new GlideArchiveRecord().archive(gr1);
                            }
                        }
                    } else {
                        var gr2 = new GlideRecord(arr.table);
                        gr2.addQuery(arr.getValue('element').tostring(), gr.sys_id);
                        gr2.query();
                        while (gr2.next()) {
                            new GlideArchiveRecord().archive(gr2);
                        }
                    }
                }
            }
        }
    },    type: 'CustomGlideArchiver'
}; */
 calling this from ui action