- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2020 07:21 AM
I want to upload a .CSV file from a folder to Servicenow. How would I go about doing that via code?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2020 07:28 AM
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');
}
Hope this helps. Please mark my answer correct and helpful.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2020 07:28 AM
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');
}
Hope this helps. Please mark my answer correct and helpful.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2020 09:49 AM
Follow up question, is there away to import data without attaching it first? Id like to get the .CSV file data and import all within code. I am using midserver code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2020 07:44 AM
Where is this script going to run? In ServiceNow or do you have a external tool like a MoveIT job or other scheduled job on some server some place that is going to run this script?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2020 08:51 AM
The script is on the Midserver in ServiceNow. I want create a schedule to grab a file from a pre-defined file system location and import it to use it for task generation.