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.

Pulling data from archived tables

Darlene York
Tera Contributor

I am building a catalog item that will allow a user to select an archived CI from an archive table to have it restored.

ar_cmdb_ci_aix_server

ar_cmdb_ci_cassandra_instance

ar_cmdb_ci_db_db2_instance

ar_cmdb_ci_db_mssql_instance

ar_cmdb_ci_db_ora_instance

ar_cmdb_ci_db_postgresql_instance

ar_cmdb_ci_display_hardware

ar_cmdb_ci_esx_server

ar_cmdb_ci_handheld_computing

ar_cmdb_ci_ibm_frame

ar_cmdb_ci_ip_camera

ar_cmdb_ci_ip_firewall

ar_cmdb_ci_ip_router

ar_cmdb_ci_ip_switch

ar_cmdb_ci_lb_netscaler

ar_cmdb_ci_linux_server

ar_cmdb_ci_mfp_printer

ar_cmdb_ci_net_app_server

ar_cmdb_ci_pc_hardware

ar_cmdb_ci_personal_printer

ar_cmdb_ci_printer

ar_cmdb_ci_server

ar_cmdb_ci_wap_network

ar_cmdb_ci_win_server

ar_u_cmdb_ci_atm

ar_u_cmdb_ci_instant_issue_printer

ar_u_cmdb_ci_secure_usb

ar_u_cmdb_ci_tcr

 

The goal is to have a catalog variable called archived_ci load all the ci’s from the above tables. 

 

I did a script include that has all the tables in it.

var ArchivedCIs = Class.create();

ArchivedCIs.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getArchivedCIs: function() {

        var tables = [

            'ar_cmdb_ci_aix_server',

            'ar_cmdb_ci_cassandra_instance',

            'ar_cmdb_ci_db_db2_instance',

            'ar_cmdb_ci_db_mssql_instance',

            'ar_cmdb_ci_db_ora_instance',

            'ar_cmdb_ci_db_postgresql_instance',

            'ar_cmdb_ci_display_hardware',

            'ar_cmdb_ci_esx_server',

            'ar_cmdb_ci_handheld_computing',

            'ar_cmdb_ci_ibm_frame',

            'ar_cmdb_ci_ip_camera',

            'ar_cmdb_ci_ip_firewall',

            'ar_cmdb_ci_ip_router',

            'ar_cmdb_ci_ip_switch',

            'ar_cmdb_ci_lb_netscaler',

            'ar_cmdb_ci_linux_server',

            'ar_cmdb_ci_mfp_printer',

            'ar_cmdb_ci_net_app_server',

            'ar_cmdb_ci_pc_hardware',

            'ar_cmdb_ci_personal_printer',

            'ar_cmdb_ci_printer',

            'ar_cmdb_ci_server',

            'ar_cmdb_ci_wap_network',

            'ar_cmdb_ci_win_server',

            'ar_u_cmdb_ci_atm',

            'ar_u_cmdb_ci_instant_issue_printer',

            'ar_u_cmdb_ci_secure_usb',

            'ar_u_cmdb_ci_tcr'

        ];

 

        var result = [];

        for (var i = 0; i < tables.length; i++) {

            var gr = new GlideRecordSecure(tables[i]); // Secure for cross-scope

            gr.addQuery('install_status', '7'); // Retired

            gr.query();

 

            var count = 0;

            while (gr.next()) {

                result.push({ label: gr.getDisplayValue(), value: gr.sys_id.toString() });

                count++;

            }

 

            // Debug log for each table

            gs.info('ArchivedCIs Debug: Table ' + tables[i] + ' returned ' + count + ' records.');

        }

 

        // Sort alphabetically

        result.sort(function(a, b) { return a.label.localeCompare(b.label); });

 

        return JSON.stringify(result);

    },

 

    type: 'ArchivedCIs'

});

 

Then on  the catalog item I have a onload client script

function onLoad() {

    var ga = new GlideAjax('ArchivedCIs');

    ga.addParam('sysparm_name', 'getArchivedCIs');

    ga.getXMLAnswer(function(response) {

        console.log('Raw response from Script Include:', response);

        try {

            var choices = JSON.parse(response);

            console.log('Parsed choices:', choices);

 

            if (!choices.length) {

                console.warn('No archived CIs returned. Check Script Include or data.');

            }

 

            choices.forEach(function(choice) {

                g_form.addOption('archived_ci', choice.value, choice.label);

            });

        } catch (e) {

            console.error('Error parsing archived CI list:', e);

        }

    });

}

 

According to my logs all those tables are showing zero ci’s.

 

Is there a better approach to this

 

Thank you

DarleneYork_0-1762980109407.png

 

0 REPLIES 0