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

Ankur Bawiskar
Tera Patron
Tera Patron

@Priyanka145 

unfortunately you cannot achieve this using any OOB logic/configuration.

You need to determine just before your scheduled import in that SFTP file path which one is the latest and update the file path accordingly.

1) run scheduled job 30mins before your scheduled import and determine which file is latest and update the data source file path

2) when scheduled import runs it will fetch the file

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

Hi @Ankur Bawiskar 

Could you please guide me with the steps for scheduled job how to determine latest file

@Priyanka145 

will the complete file name change or just the date in file name will change?

based on that you can define the logic

check this

https://www.servicenow.com/community/developer-forum/retrieve-sftp-data/m-p/1631355/page/2 

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

Hi @Ankur Bawiskar  , File name wont change, date in the file name changes.