Add a script condition in Archive restore record and related records

Shirley Bribon
Tera Contributor

Hello everyone,

 

Can you help me add a  condition to this script:

need to add a condition created is more than 7 years to bulk update Archive change request

 

 

Issue :Before we can delete archive change request it needs to be manually click the  "restore record and related records" I don't know how to bulk update this so once the archive change request has been restored record and related records, we will be able to delete from System Data Management /Delete jobs.

 

var archiveLog = new GlideRecord('sys_archive_log');
archiveLog.addQuery('id', current.sys_id);
archiveLog.addQuery('from_table', 'ar_incident'); // Add a query for the 'from_table' field to match 'ar_incident'
archiveLog.addNullQuery('restored');
archiveLog.query();

if (archiveLog.next()) {
var und = new GlideArchiveRestore().restore(archiveLog.sys_id);

if (!und) {
gs.addInfoMessage(gs.getMessage("The restore failed"));
action.setRedirectURL(current);
} else {
var restoredRecord = new GlideRecord('ar_incident'); // Change the table to 'ar_incident'

if (restoredRecord.get(archiveLog.id)) {
action.setRedirectURL(restoredRecord);
} else {
gs.addInfoMessage(gs.getMessage("Could not locate the restored record"));
action.setRedirectURL(current);
}
}
} else {
action.setRedirectURL(current);
gs.addErrorMessage(gs.getMessage("No active archive log entry found. Record probably already restored"));
}

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hello @Shirley Bribon ,

 

To add a condition that checks if the record was created more than 7 years ago, you will need to include a query that filters based on the creation date. The following script includes this condition:

var SEVEN_YEARS_IN_MILLISECONDS = 7 * 365 * 24 * 60 * 60 * 1000; // Approximation, doesn't account for leap years
var sevenYearsAgo = new GlideDateTime();
sevenYearsAgo.subtract(SEVEN_YEARS_IN_MILLISECONDS);

var archiveLog = new GlideRecord('sys_archive_log');
archiveLog.addQuery('id', current.sys_id);
archiveLog.addQuery('from_table', 'ar_incident'); 
archiveLog.addNullQuery('restored');
archiveLog.addQuery('sys_created_on', '<', sevenYearsAgo); 
archiveLog.query();

if (archiveLog.next()) {
    var und = new GlideArchiveRestore().restore(archiveLog.sys_id);

    if (!und) {
        gs.addInfoMessage(gs.getMessage("The restore failed"));
        action.setRedirectURL(current);
    } else {
        var restoredRecord = new GlideRecord('ar_incident'); 

        if (restoredRecord.get(archiveLog.id)) {
            action.setRedirectURL(restoredRecord);
        } else {
            gs.addInfoMessage(gs.getMessage("Could not locate the restored record"));
            action.setRedirectURL(current);
        }
    }
} else {
    action.setRedirectURL(current);
    gs.addErrorMessage(gs.getMessage("No active archive log entry found. Record probably already restored or not older than 7 years"));
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

 

View solution in original post

1 REPLY 1

Amitoj Wadhera
Kilo Sage

Hello @Shirley Bribon ,

 

To add a condition that checks if the record was created more than 7 years ago, you will need to include a query that filters based on the creation date. The following script includes this condition:

var SEVEN_YEARS_IN_MILLISECONDS = 7 * 365 * 24 * 60 * 60 * 1000; // Approximation, doesn't account for leap years
var sevenYearsAgo = new GlideDateTime();
sevenYearsAgo.subtract(SEVEN_YEARS_IN_MILLISECONDS);

var archiveLog = new GlideRecord('sys_archive_log');
archiveLog.addQuery('id', current.sys_id);
archiveLog.addQuery('from_table', 'ar_incident'); 
archiveLog.addNullQuery('restored');
archiveLog.addQuery('sys_created_on', '<', sevenYearsAgo); 
archiveLog.query();

if (archiveLog.next()) {
    var und = new GlideArchiveRestore().restore(archiveLog.sys_id);

    if (!und) {
        gs.addInfoMessage(gs.getMessage("The restore failed"));
        action.setRedirectURL(current);
    } else {
        var restoredRecord = new GlideRecord('ar_incident'); 

        if (restoredRecord.get(archiveLog.id)) {
            action.setRedirectURL(restoredRecord);
        } else {
            gs.addInfoMessage(gs.getMessage("Could not locate the restored record"));
            action.setRedirectURL(current);
        }
    }
} else {
    action.setRedirectURL(current);
    gs.addErrorMessage(gs.getMessage("No active archive log entry found. Record probably already restored or not older than 7 years"));
}

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera