How to insert records on incident table using transform script ?

Ujjwala Wagh
Kilo Contributor

Hello All,

I have been trying to load data into the system via inbound email attachment, but on transform incident table is not creating records, could anyone guide me for this ? Should I create transform script for this, if yes please suggest me the approach.

Thank you in advance!

Regards,

Ujjwala

find_real_file.png

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Unfortunately you didn't share how are you loading the data?

what's the transform map? what field did you configure with coalesce = true?

Regards
Ankur

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

Hello Sir,

I have followed the three steps and I am using below scripts. The only issue is records are not getting inserted into an incident table. Please sir, if you could suggest me some changes here, it is going to help me a lot.

Step 1. Create the Import Set Table and Transform Map(s)

Import set Table Name: Cafe Inventory Tickets

Table Transform Map: Data Upload for Inventory Tickets

Target Table: Incident

Step 2. Create the Inbound Email Action

Type : New

Target Table : Data source [sys_data_source]

current.initialize();
if (email.from.indexOf("") > -1) {
    (function runAction(current, event, email, logger, classifier) {

        // modify the next 2 lines with the names of your Import Set Table.
        var importSetName = "Cafe Inventory Tickets";
        var importSetLabel = "u_cafe_inventory_tickets";

        var applicationScope = "Global";

        // setup the data source
        current.name = importSetName + " " + gs.nowDateTime();
        current.import_set_table_name = importSetLabel;
        current.import_set_table_label = importSetName;
        current.type = "File";
        current.format = "Excel";
        current.sheet_number = 1;
        current.header_row = 1;
        current.file_retrieval_method = "Attachment";
        current.sys_package.setDisplayValue(applicationScope);
        current.sys_scope.setDisplayValue(applicationScope);
        var myDS = current.insert();

        new global.EmailFileImportUtils().scheduleImport(myDS);

    })(current, event, email, logger, classifier);

}

Step 3. Create the Script include

var EmailFileImportUtils = Class.create();  
EmailFileImportUtils.prototype = {  
    initialize: function() {  
    },  
      
    scheduleImport: function(dataSourceID) {  
        /* 
         * Create scheduled job to process import 
         * 
         * will generate an import data source, however the attachment 
         * isn't copied to the data source until after the record insert.
         * The code below creates a scheduled job to process the file import
         * 30 seconds later so that attachment has time to be copied to the
         * data source from the email. 
         */  
          
        var schRec = new GlideRecord("sys_trigger");  
        schRec.name = "Load Data Source: " + dataSourceID;  
        schRec.trigger_type = 0; // Run Once  
        schRec.script = "new global.EmailFileImportUtils().loadImportSet('" + dataSourceID + "')";  
          
        var nextAction = new GlideDateTime();  
        nextAction.addSeconds(30); // 30 seconds however this can be changed.  
        schRec.next_action = nextAction;  
        schRec.insert();  
    },  
      
    loadImportSet: function(dataSourceID) {  
        // Get Datasource Record  
        var dataSource = new GlideRecord("sys_data_source");  
        dataSource.get(dataSourceID);  
          
        // Process data source file  
        var loader = new GlideImportSetLoader();  
        var importSetRec = loader.getImportSetGr(dataSource);  
        var ranload = loader.loadImportSetTable(importSetRec, dataSource);  
        importSetRec.state = "loaded";  
        importSetRec.update();  
          
        // Transform import set  
        this._doTransform(importSetRec); 
    },  
      
    _doTransform: function(set){
        var importSetRun = new GlideImportSetRun(set.getUniqueValue());
        var importLog = new GlideImportLog(importSetRun, set.data_source.name);
        var ist = new GlideImportSetTransformer();

        ist.setLogger(importLog);
        ist.setImportSetRun(importSetRun);
        ist.transformAllMaps(set);
     },
    type: 'EmailFileImportUtils'  
};

When I checked import set tables , I observed that records are getting inserted but target table is showing empty

I have attached an excel file from which I have been trying to fetch unique ids so that using those unique ids separate incidents along with short description, category and subcategory, assignment group will be created in the system.

Thank you in advance!

Ujjwala