import set not getting created through business rule

keshav77
Tera Contributor

I am trying to create import set through business rule but it is not getting created please help me with this below is my script. it is not going inside var loader = new GlideImportSetLoader();

(function executeRule(current, previous /*null when async*/) {
    gs.info('--- Script Execution Started ---');

    // Step 1: Create a new Data Source
    var ds = new GlideRecord('sys_data_source');
    ds.initialize();
    ds.name = "Dynamic_" + gs.nowDateTime();
    ds.import_set_table_name = 'u_myspecial';
    ds.type = 'File';
    ds.format = 'Excel';
    ds.file_retrieval_method = 'Attachment';
    ds.header_row = 2;
    ds.sheet_number = 1;
    var dataSourceSysId = ds.insert();
    gs.info('[Step 1] Data Source created with sys_id: ' + dataSourceSysId);

    // Step 2: Copy the attachment from the RITM to the new data source
    var at = new GlideSysAttachment();
    var newAttachmentSysId = at.copy(
        'sc_req_item',
        current.getUniqueValue(),
        'sys_data_source',
        dataSourceSysId
    );
    if (!newAttachmentSysId) {
        gs.error('[Step 2] Attachment copy failed for RITM: ' + current.number);
        return;
    }
    gs.info('[Step 2] Attachment copied successfully: ' + newAttachmentSysId);

    var dsUpdate = new GlideRecord('sys_data_source');
    if (dsUpdate.get(dataSourceSysId)) {
        dsUpdate.attachment = newAttachmentSysId;
        dsUpdate.update();
        gs.info('[Step 2] Data source updated with attachment.');
    } else {
        gs.error('[Step 2] Failed to retrieve data source for update.');
        return;
    }

    // Step 3: Load and process the Import Set
    gs.info('[Step 3] Starting import set loader...');
    var loader = new GlideImportSetLoader();
    if (!loader) {
        gs.error('[Step 3] GlideImportSetLoader not initialized.');
        return;
    }

    var grImportSet = loader.getImportSetGr('11cdcaf1c3be6e50478af73ed4013153');
    if (!grImportSet || !grImportSet.isValidRecord()) {
        gs.error('[Step 3] Import Set not created. Check data source or attachment.');
        return;
    }
    gs.info('[Step 3] Import Set record created: ' + grImportSet.sys_id);

    var ranload = loader.loadImportSetTable(grImportSet, '11cdcaf1c3be6e50478af73ed4013153');
    if (!ranload) {
        gs.error('[Step 3] Failed to load data into Import Set table.');
        return;
    }
    gs.info('[Step 3] Data loaded into Import Set table.');

    grImportSet.state = "loaded";
    grImportSet.update();
    gs.info('[Step 3] Import Set state updated to "loaded".');

    // Step 4: Execute Transform Map
    var transformMapSysID = '059f64d4c37a6650478af73ed40131ec';
    gs.info('[Step 4] Starting transform with map: ' + transformMapSysID);

    var transformWorker = new GlideImportSetTransformerWorker(grImportSet.sys_id, transformMapSysID);
    transformWorker.setBackground(true);
    transformWorker.start();
    gs.info('[Step 4] Transform Worker started for Import Set: ' + grImportSet.sys_id);

    gs.info('--- Script Execution Completed ---');
})(current, previous);
 
2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@keshav77 

why are you create a fresh data source and then copying file?

why not use the existing one and then simply copy and trigger the transformation?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar  because this getting called through requested item. if multiple people hit that item together that may create issue I think?