how can we import excel file into import set table using script and excel cells not columns titles

Nessrine
Tera Contributor

Hi,

 

I need to import an Excel file into an existing import set table using a script (a business rule is triggered when attaching the Excel file). However, this file can be in different languages each time, so I suppose I have to use the cell numbers, not the column titles. Does anyone have an idea how to do that?

 

Thank you for your help

2 REPLIES 2

Yashsvi
Kilo Sage

Hi @Nessrine,

please try below script:

// Example script to process an attached Excel file
var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_name', 'your_table_name');
gr.addQuery('table_sys_id', current.sys_id); // Assuming current is the current record context
gr.query();

if (gr.next()) {
    var attachmentSysId = gr.getUniqueValue();
    var importSetAPI = new GlideImportSet();
    var importSet = importSetAPI.getImportSetBySysID('your_import_set_sys_id'); // Replace with your import set sys_id

    // Set the source XML to the attachment content
    var attachment = new GlideSysAttachment();
    var fileContent = attachment.getContent(attachmentSysId);
    importSet.setXMLData(fileContent);

    // Process the import set
    var importSetRow = new GlideImportRow(importSet, importSet.getRowID());
    var rowSysId = importSetRow.insert();

    if (rowSysId) {
        gs.info('Excel file imported successfully.');
    } else {
        gs.error('Failed to import Excel file.');
    }
}

Thank you, please make helpful if you accept the solution.

I have been looking for GlideImportSet() in the ServiceNow documentation for a while and I have not find it. Are you sure this is correct?