How to fetch latest file from the file path in data sources

Priyanka145
Tera Contributor

Hi, I have created a scheduled import to run the transform map via data source. How to fetch the dynamically based on latest updated file from the file path in SFTP folder in data source. If the file path is having multiple files, it has to pick the latest added/updated file. Please guide me how to achieve it

1 ACCEPTED SOLUTION

@Priyanka145 

update as this

var grd = new GlideRecord("sys_data_source");
grd.addQuery('sys_id', 'datasource sysid');
grd.query();
if (grd.next()) {
var currentdate = new GlideDateTime().getDate().getByFormat('dd-MM-YYYY');
grd.file_path = '/ServiceNow/ServiceNow_' + currentdate + '.csv';
grd.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

8 REPLIES 8

@Priyanka145 

then you can just run scheduled job before your scheduled import and get the date and update the file path

can you share how the file path looks like i.e. file name and will the date change based on today's date etc

Example: today the file name will be hello_2022_11_02 and tomorrow it will be hello_2022_11_03

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

Hi @Ankur Bawiskar 

I tried below script and I am getting the file name as ServiceNow_YYYY-MM-DD format. But How can I get the format in DD-MM-YYYY . Please guide

 

var grd = new GlideRecord("sys_data_source");
grd.addQuery('sys_id', 'datasource sysid');
grd.query();
if (grd.next()) {
var currentdate = new GlideDateTime().getDate();
grd.file_path = '/ServiceNow/ServiceNow_' + currentdate + '.csv';
grd.update();
}

@Priyanka145 

update as this

var grd = new GlideRecord("sys_data_source");
grd.addQuery('sys_id', 'datasource sysid');
grd.query();
if (grd.next()) {
var currentdate = new GlideDateTime().getDate().getByFormat('dd-MM-YYYY');
grd.file_path = '/ServiceNow/ServiceNow_' + currentdate + '.csv';
grd.update();
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

alexgg57
Tera Expert

I am sorry to bring this topic up.

I have slightly different question:

instead of single *.csv file per run, we use *.zip file which contains multiple *.csv.

How to get: which of unzipped filename is currently in the mapping transform process?

Is it possible?