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.

Adding excel records in sequential manner after import

MaharshiC
Tera Contributor

Hi,

 

I am allowing the users to create bulk records at once when they upload the excel file. For this I am using a business rule  to create records in my staging table and then from staging table to my target table using a transform map. This is working fine but the users are reporting one issue -- the records are not getting inserted in the same sequence as present in the excel. How do I solve this? Same sequence here means new records will create in the target table in the same sequence as present in the excel.

The business rule script -- 

(function executeRule(current, previous /*null when async*/ ) {

    var tableName = "x_iem_tqa_tqa_staging_request_items"; //Staging Table
    var shortDescription = "Data load TQA";
    var impSet = new GlideRecord('sys_import_set');
    impSet.initialize();
    impSet.state = 'loading';
    impSet.mode = 'asynchronous';
    impSet.table_name = tableName;
    impSet.short_description = shortDescription;
    impSet.insert();
    var importSys_ID = impSet.getUniqueValue();

    var sys = '';
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.sys_id);
    gr.addEncodedQuery('file_nameSTARTSWITHTQA');
    gr.query();
    if (gr.next()) {
        sys = gr.sys_id;
		
    }
	

    var parser = new sn_impex.GlideExcelParser();
    var attachment = new GlideSysAttachment();
    var attachmentStream = attachment.getContentStream(sys);
    parser.setSource(attachmentStream);

    if (parser.parse()) {
        var headers = parser.getColumnHeaders();
        var header1 = headers[0];
        var header2 = headers[1];

        while (parser.next()) {
            var row = parser.getRow();
            var impSetRow = new GlideRecord('x_iem_tqa_tqa_staging_request_items');
            impSetRow.initialize();
            impSetRow.sys_import_state = 'pending';
            impSetRow.sys_import_set = importSys_ID;
            impSetRow.setValue('request_item_type', row[header1]);
            impSetRow.setValue('tag_report_anomaly_other', row[header2]);
            impSetRow.setValue('work_request', current.work_request);
            impSetRow.setValue('tqa_work_request_task', current.sys_id);

            impSetRow.insert();

        }
    } else
        gs.info(parser.getErrorMessage());
       

    var impset = new GlideRecord('sys_import_set');
    impset.get(importSys_ID);
    if (impset) {
        impset.state = 'loaded';
        impset.update();
        var transformer = new GlideImportSetTransformer();
        transformer.transformAllMaps(impSet);
    }

})(current, previous);
0 REPLIES 0