Dynamically change the import file name from a SFTP data source

Tena
Kilo Contributor

I want to import a data set from SFTP on a daily basis where the file name changes daily like fileddmmyy.csv.

Any recommendation to achieve this ?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron

Hi,

you need to update the file name in the data source just before the sftp job runs

Use schedule job which runs just 5-10mins prior to the scheduled data import for this sftp

sample script below

Note: ensure you give valid data source sys_id

valid file path

the below code will help you give file name daily as file061820.csv, file061920.csv

ensure you use valid file path

var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id',''); // give here the data source sys_id
gr.query();
if(gr.next()){

var gdt = new GlideDateTime();
var dateTime = gdt.toString();
var arr = dateTime.split(' ');

var yearDate = arr[0].split('-');

var dd = yearDate[1];

var mm = yearDate[2];

var year = yearDate[0];

var yearPart = year.substring(0,2);

var str = 'file' + dd + mm + yearPart + '.csv';

gr.file_path = str;

gr.update();
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron

Hi,

you need to update the file name in the data source just before the sftp job runs

Use schedule job which runs just 5-10mins prior to the scheduled data import for this sftp

sample script below

Note: ensure you give valid data source sys_id

valid file path

the below code will help you give file name daily as file061820.csv, file061920.csv

ensure you use valid file path

var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id',''); // give here the data source sys_id
gr.query();
if(gr.next()){

var gdt = new GlideDateTime();
var dateTime = gdt.toString();
var arr = dateTime.split(' ');

var yearDate = arr[0].split('-');

var dd = yearDate[1];

var mm = yearDate[2];

var year = yearDate[0];

var yearPart = year.substring(0,2);

var str = 'file' + dd + mm + yearPart + '.csv';

gr.file_path = str;

gr.update();
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi @Ankur Bawiskar , 

 

I tried this same code on my instance and for some reason the scheduled job is not updating de data source file path. When I execute now the schedueled job nothing happens. 

 

Can you please help me understand what's going on? Thank you a lot!