Please review my record producer script

Dash2
ServiceNow Employee
ServiceNow Employee

Hi all,

 

I have the following record producer built on the sys_data_source table. All the artifacts required to run an import in the script can be retrieved from the u_asset_wizard table (import set table name, transform map, and data source), all reference fields.

 

The issue I'm facing right now is that the system keeps generating a new data source. I actually want it to use the data source from the u_asset_wizard table (u_datasource). After all the validations, I want it to get the data source (existingSource) from the Wizard table, delete all existing attachments, copy the new attachment to the data source, and then proceed with the import.

@Ankur Bawiskar 

My script is below:

// Verify attachment is included and in correct format
current.setAbortAction(true);
var type = producer.import_type;
var num = producer.sheet_number;
var gr2 = new GlideRecord("sys_attachment");
gr2.addQuery("table_sys_id", current.sys_id);
var oC = gr2.addQuery("table_name", "sys_data_source");
oC.addOrCondition("table_name", "sc_cart_item");
gr2.query();

if (!gr2.next()) {
    gs.addErrorMessage("You must attach a file to submit. Your import submission has been aborted.");
    current.setAbortAction(true);
    producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=6a938ef1c3efca90a8acde5bb001310d";
} else {
    // Get the GlideRecord for the selected import type
    var gr = new GlideRecord('u_asset_import_wizard');
    gr.addQuery('u_import_type', type);
    gr.query();
    if (gr.next()) {
        if (gr2.getRowCount() > 1) {
            gs.addErrorMessage("You may only attach one file at a time for this import wizard. Your import submission has been aborted.");
            current.setAbortAction(true);
            producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=6a938ef1c3efca90a8acde5bb001310d";
        } else {
            // Check if u_validate is true and filename contains the version number
            if (gr.u_validate && !gr2.file_name.includes(gr.u_version)) {
                gs.addErrorMessage("The file you've attached is outdated. Please download the current version from the table below.");
                current.setAbortAction(true);
                producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=6a938ef1c3efca90a8acde5bb001310d";
            } else {
                // Check to make sure the file format is correct on the attachment
                var passedFormatCheck = false;
                if (gr.u_file_format == 'CSV' && gr2.file_name.endsWith('.csv')) {
                    passedFormatCheck = true;
                } else if (gr.u_file_format == 'Excel' && (gr2.file_name.endsWith('.xls') || gr2.file_name.endsWith('.xlsx'))) {
                    passedFormatCheck = true;
                } else {
                    gs.addErrorMessage("This import type is expecting submission of a " + gr.u_file_format + " file, but a different file format was attached. Your import submission has been aborted.");
                    current.setAbortAction(true);
                    producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=6a938ef1c3efca90a8acde5bb001310d";
                }

                if (passedFormatCheck) {
                    // Set data source record fields from the import wizard record
                    var datasourceSysid = gr.u_datasource.sys_id;
                    var existingSource = new GlideRecord('sys_data_source');
                    existingSource.get(datasourceSysid);
                    existingSource.u_user = gs.getUserID;
                    existingSource.last_run_datetime = new GlideDateTime();
                    existingSource.update();

                    var attachmentCopy = new GlideSysAttachment();
                    attachmentCopy.copy('sc_cart_item', current.sys_id, 'sys_data_source', existingSource);

                    // Process file into data source record
                    var loader = new GlideImportSetLoader();
                    var importSetRec = loader.getImportSetGr(existingSource);

                    // Import data from data source to import set table (based on form import type selection record)
                    var ranload = loader.loadImportSetTable(importSetRec, existingSource);
                    importSetRec.state = "loaded";
                    importSetRec.update();

                    // Start appropriate transform map (will have the logic for logging exceptions within the transform map scripts, and will trigger an email once complete to the import submitter with an outline of the logged errors and warnings)
                    var transformMapID = gr.u_transform_map;
                    var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id, transformMapID);
                    transformWorker.setBackground(true);
                    transformWorker.start();

                    // Inform the user that an email outlining the status of the import will be sent once the import is complete
                    gs.addInfoMessage("Your import file has been submitted. An email will be sent to you once the import is completed to outline any errors or warnings encountered while importing.");
                    producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=6a938ef1c3efca90a8acde5bb001310d";

                }
            }
        }
    } else {
        gs.addErrorMessage('Something went wrong with the import. Please contact a system admin to investigate.');
        producer.redirect = "com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=6a938ef1c3efca90a8acde5bb001310d";
    }

    // Since we inserted the data source already, abort additional insert by record producer
    current.setAbortAction(true);
}

  

1 REPLY 1

Omkar Mone
Mega Sage

Is this the same script from SNGuru? 

https://servicenowguru.com/imports/simplifying-data-imports-parties/ and the object that you are passing to the GlideImportSet API is not the current object which is of Data source, I think hence its creating a new one everytime.