How do I upload a .CSV file to Servicenow via Scripts?

Ccart
Giga Expert

I want to upload a .CSV file from a folder to Servicenow. How would I go about doing that via code? 

1 ACCEPTED SOLUTION

Vishwa Prakash
Kilo Expert

Hi,

You may try this. Also I have attached a link which might help you achieve your requirement.

 var type = {};
type.schedule = 'u_imp_tmpl_u_term_phr_empl_mvs_ids'; //Display name for scheduled import  -- eb9f2dae6f46a60051281ecbbb3ee4a5
type.table = 'u_imp_tmpl_u_term_phr_empl_mvs_ids'; //Import table name

gs.log('0. Process File Start');

if(type.schedule != '' && type.table != '') {
    gs.log('1. Setting up data source');
    current.name = type.schedule + ' ' + gs.nowDateTime(); //append date time to name of data source for audit
    current.import_set_table_name = type.table;
    current.import_set_table_label = "";
    current.type= "File";
    current.format = "CSV";  //"Excel CSV (tab)";
    current.header_row = 1;
    current.file_retrieval_method = "Attachment";

    var myNewDataSource = current.insert();

    gs.log('2. Data source inserted with sys_id - ' + myNewDataSource);

    //point the scheduled import record to the new data source
    var gr = new GlideRecord ('scheduled_import_set');

    gr.addQuery('name', type.schedule);
    gr.query();

    if (gr.next()) {
        gs.log('3. Found Scheduled Import definition');
        gr.data_source = myNewDataSource;
        gr.update();
        gs.log('4. Scheduled Import definition updated to point to data source just added');

        //Packages.com.snc.automation.TriggerSynchronizer.executeNow(gr);
        //Execute a scheduled script job
        SncTriggerSynchronizer.executeNow(gr);
    } else {
        // add error conditions to email somebody that this has occurred
        gs.log('5. ERROR - Unable to locate scheduled import definition. Please contact your system administrator');
    }

    gs.log('6. Inbound email processing complete');
    //gs.eventQueue("ep_server_processed",current);
    event.state="stop_processing";
} else {
    gs.log('7. Inbound email processing skipped');
}

https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/import-sets/task/...

Hope this helps. Please mark my answer correct and helpful. 

Thanks

View solution in original post

6 REPLIES 6

Gaurav Shirsat
Mega Sage

Hello Ccart 

You can upload the .csv file to service now using the import set.

when we want to insert the data into the service now manually,we use import set.

even you can import data at specific interval of time.

https://docs.servicenow.com/bundle/orlando-platform-administration/page/administer/import-sets/task/...

Application Naviagtor>Import Set>choose file>select from your drive

find_real_file.png

Please Mark Correct and Helpful

Thanks and Regards

Gaurav Shirsat