The CreatorCon Call for Content is officially open! Get started here.

Load Data. Script.

Alex153
Tera Contributor

Hi guys,

Do you know how to create Load data using script? Or what's the name of a table where I can find Import set tables?

find_real_file.png

9 REPLIES 9

Hi,

are you saying each time you wish to create new import set table?

Regards
Ankur

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

Only once but using script. And use this table after.

Hi,

If you wish to create table only once and use it multiple times later then why to use script to create table?

Regards
Ankur

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

Snehangshu Sark
Mega Guru

Hi,

 

Here is the path to find all import set tables -   System Definition -> Tables -> query for Import Set.

You'd find the list of import set tables

 

Mark it Helpful, if it works.

Thank you

SS

Snehangshu Sark
Mega Guru

Hi,

 

I've been using the below Script Include to load data programmatically for several projects, you can try this.

Few things you need to change here - 

- keep the excel or CSV file in the Data source table

- make sure the import set table name is updated in the data source record.

- In the script, dataSourceID is the sys_id of that data source (that you want to load)

- create a transform map beforehand.

- In the script, transformMapID is the sys_id of the transform map (that you want to use to map and load data)

- put the below script into a Script Include and you can call it from any server-side script (such as - inbound action, BR, scheduled job etc.)

 

var EmailFileImportUtils = Class.create();
EmailFileImportUtils.prototype = {

    scheduleImport: function(dataSourceID, transformMapID) {
        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 + "', '" + transformMapID + "')";

        var nextAction = new GlideDateTime();
        nextAction.addSeconds(30); // 30 seconds should be enough time however this can be changed.  
        schRec.next_action = nextAction;
        schRec.insert();
    },

    loadImportSet: function(dataSourceID, transformMapID) {
        // 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  
        var transformWorker = new GlideImportSetTransformerWorker(importSetRec.sys_id, transformMapID);
        transformWorker.setBackground(true);
        transformWorker.start();
    },
};

 

Regards,

SS

 

Please mark an appropriate response as correct if my answer replied to your question.