Download the attachments from old records

JRY
Mega Guru

Hi,

 

I created a custom button in the HR Agent workspace that allows you to download multiple attachments with a single click. It is working properly, however I am facing one problem: there are a few old records with attachments that do not display button. I tried to update the field value using the fix script, but had no luck. Can you tell me how to do this?

(function() {
    // Parent table
    var parentTable = 'sn_hr_core_case';
    // Child tables
    var childTables = [
        'sn_hr_core_case_benefits',
        'sn_hr_core_case_compensation',
        'sn_hr_core_case_corporate_communication',
        'sn_hr_core_case_corporate_hr',
        'sn_hr_core_case_dchr',
        'sn_hr_core_case_ethics',
        'sn_hr_core_case_global_mobility',
        'sn_hr_core_case_investigation',
        'sn_hr_core_case_leave_of_absence',
        'sn_hr_core_case_operations',
        'sn_hr_core_case_payroll',
        'sn_hr_core_case_performance',
        'sn_hr_core_case_relations',
        'sn_hr_core_case_systems_support',
        'sn_hr_core_case_talent_management',
        'sn_hr_core_case_total_rewards',
        'sn_hr_core_case_unemployment',
        'sn_hr_core_case_workforce_admin',
        'sn_hr_core_covid_cases',
        'sn_hr_core_fleet_hr_case',
        'sn_hr_core_hrsc_support',
        'sn_hr_core_hr_field_changes',
        'sn_hr_core_hr_hcm_case'
        // Add more child tables as needed
    ];

    // Function to update the u_has_attachments field for a given table
    function updateAttachmentsField(tableName) {
        var gr = new GlideRecord(tableName);
        gr.query();
        while (gr.next()) {
            var attachmentGR = new GlideRecord('sys_attachment');
            attachmentGR.addQuery('table_sys_id', gr.sys_id);
            attachmentGR.addQuery('table_name', tableName);
            attachmentGR.query();
            if (attachmentGR.hasNext()) {
                gr.u_has_attachments = true;
                gr.update();
            }
        }
    }

    // Update parent table
    updateAttachmentsField(parentTable);

    // Update child tables
    for (var i = 0; i < childTables.length; i++) {
        updateAttachmentsField(childTables[i]);
    }

    gs.log('Fix script to update u_has_attachments field completed successfully.');

})();

 

JRY_0-1721751380928.png

 

1 ACCEPTED SOLUTION

Chaitanya Redd1
Tera Guru

Hello,

 

You can try below script. I have tested this will work.

 

// Define the table where you want to add the button
var tableName = 'sn_hr_core_case_payroll'; // Replace with your actual table name

// Query the attachments table for records with attachments
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', tableName);
grAttachment.query();

while (grAttachment.next()) {
    var recordSysId = grAttachment.getValue('table_sys_id');

    // Update the record to trigger the display of the button
    var grRecord = new GlideRecord(tableName);
    if (grRecord.get(recordSysId)) {
        grRecord.setWorkflow(false); // Prevent workflows from running
        // Add any specific updates needed to show the button
        grRecord.u_has_attachments = true;
        grRecord.update();
    }
}

 
Thanks,

Chaitanya

View solution in original post

2 REPLIES 2

Chaitanya Redd1
Tera Guru

Hello,

 

You can try below script. I have tested this will work.

 

// Define the table where you want to add the button
var tableName = 'sn_hr_core_case_payroll'; // Replace with your actual table name

// Query the attachments table for records with attachments
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', tableName);
grAttachment.query();

while (grAttachment.next()) {
    var recordSysId = grAttachment.getValue('table_sys_id');

    // Update the record to trigger the display of the button
    var grRecord = new GlideRecord(tableName);
    if (grRecord.get(recordSysId)) {
        grRecord.setWorkflow(false); // Prevent workflows from running
        // Add any specific updates needed to show the button
        grRecord.u_has_attachments = true;
        grRecord.update();
    }
}

 
Thanks,

Chaitanya

Thanks this worked