How to create a button to export CSV file for a table?

LinhN
Tera Contributor

Hi guys, I am writing a script to export CSV files based on the query condition. I have successfully exported the CSV file in the script background by attaching it to a specific record of the consumable table. Now I want to use UI action to create a button for the 'alm_consumable' table, and when users click this button, it will export a file. 

var ga1 = new GlideAggregate('alm_consumable');
ga1.addQuery('model_category.name', 'Salesforce Voucher');
ga1.addQuery('install_status', '10');
ga1.addQuery('assigned_to.department.u_layered_structure', '!=', '');
ga1.addQuery('u_salesforce_exam_date', '>=', '2000-01-01 00:00:00'); // Start of 1.1.2024
ga1.addQuery('u_salesforce_exam_date', '<=', '2024-12-31 23:59:59'); // End of 31.1.2024
ga1.groupBy('assigned_to.department.u_layered_structure'); // Group by layered structure
ga1.addAggregate('COUNT');
ga1.query();

var csvData = '"Type","Name","Count"\r\n'; // Add headers

var groupedDepartments = {}; // Object to store department counts
var groupedHeadquarters = {}; // Object to store headquarters counts

while (ga1.next()) {
    var layeredStructure = ga1.getValue('assigned_to.department.u_layered_structure');
    var count = parseInt(ga1.getAggregate('COUNT'), 10);

    if (layeredStructure) {
        var segments = layeredStructure.split('/'); // Safely split the layeredStructure into segments
        var headquarter = ''; // To hold the headquarter (HeadQuarter)
        var department = '';  // To hold the department (Department)
        segments.forEach(function(segment) {
            // Check if the segment includes HeadQuarter or BU, and add them as headquarter
            if (segment.includes('Headquarter') || segment.includes('BU')) {
                headquarter = segment; // Valid headquarter found
            } else if (!segment.includes('Headquarter') &&
                !segment.includes('BU') &&
                !segment.includes('Unit') &&
                !segment.includes('CFO') &&
                !segment.includes('CRO') &&
                !segment.includes('AoraNow')) {
                department = segment; // Valid department found
            }
        });

        // Combine headquarter and department into a display name for department grouping
        var displayName = '';
        if (headquarter) {
            displayName = headquarter; // If headquarter exists, use it
        }

        if (department) {
            if (displayName) displayName += '- '; // If there is a headquarter, add a separator
            displayName += department; // Add department
        }

        // Aggregate by department
        if (displayName) {
            groupedDepartments[displayName] = (groupedDepartments[displayName] || 0) + count;
        }

        // Aggregate by headquarter
        if (headquarter) {
            groupedHeadquarters[headquarter] = (groupedHeadquarters[headquarter] || 0) + count;
        }
    }
}

// Add department data to CSV
for (var departmentName in groupedDepartments) {
    csvData += '"Department","' + departmentName + '","' + groupedDepartments[departmentName] + '"\r\n';
}

// Add headquarter data to CSV
for (var headquarterName in groupedHeadquarters) {
    csvData += '"Headquarter","' + headquarterName + '","' + groupedHeadquarters[headquarterName] + '"\r\n';
}

// Attach the CSV to a consumable record
var consumableRecord = new GlideRecord('alm_consumable');
if (consumableRecord.get('sys_id', '54095c23c34692504dfdd8677a01317e')) {
    var attachment = new GlideSysAttachment();
    attachment.write(consumableRecord, 'DepartmentHeadquarterCounts.csv', 'application/csv', csvData);
}

 How do I use the script to create a button for the alm_consumable table ( checked list banner button) 

LinhN_0-1735815158130.png

 

2 REPLIES 2

Vasantharajan N
Giga Sage
Giga Sage

@LinhN - Did you try using the "UI Context Menu" to export the data in CSV and other available format from a list view


Thanks & Regards,
Vasanth

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @LinhN 

 

You want to export data from SN, you can do it OOTB, what is your use case for custom solution. 

 

AGLearnNGrow_0-1735816981397.png

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************