Change name of the file in export set

ark6
Mega Guru

Hi,

I am using export set and want to change the name of the file to be exported to mid server before exporting.

My requirement is to attach the current date to the file name, but when I do an append timestamp it appends both date and time on the file name.

Is there a way we can control this behavior and try to attach the date instead of a whole timestamp?

Regards,

Ark

18 REPLIES 18

Hi Arka,



Yes you can do one thing. In the scheduled export set there is an option of pre script and post script. there will be 2 checkboxes check those and add below scripts



Pre script:


1) Query current export set belonging to this scheduled export set and change the 'file_name' value of export set to your one.



Script:



example: file name is incident.csv and you want incident_2017-08-30.csv



var nowDate = new GlideDateTime().getDate(); // get today date



var exportSysId = ''; // hard code sysId of the export set


var exportSetGr = new GlideRecord('sys_export_set');


exportSetGr.addQuery('sys_id',exportSysId);


exportSetGr.query();


if(exportSetGr.next()){


var currentFileName = exportSetGr.file_name;



currentFileName = currentFileName.substring(0, currentFileName.lastIndexOf(".")); // this will give you the text "incident"



var modifiedFileName = currentFileName + '_' + nowDate + '.csv'; // remember to attach the extension for example I have used csv your might be another one



exportSetGr.file_name =   modifiedFileName;


exportSetGr.update();


}




Post Script:


1) Query current export set belonging to this scheduled export set and change the file_name back to earlier value.



Script:



var exportSysId = ''; // hard code sysId of the export set


var exportSetGr = new GlideRecord('sys_export_set');


exportSetGr.addQuery('sys_id',exportSysId);


exportSetGr.query();


if(exportSetGr.next()){


exportSetGr.file_name = 'incident.csv'; // since you know this post script is running for incident you can directly rename it with incident.csv


exportSetGr.update();


}




Try this and let me know whether this works.



Mark Correct if this solves your issue and also hit Like and 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,



Thanks for the reply..



I did the same exactly, what I found out is, the prescript do update the name of the file, but it does the same after it has exported the same to the mid server i.e the old file name will be there when it has exported for the first time.



But, after exporting it, it changes the file name in my export set but not in the mid server.



Actually i want to push the file name with a updated value to an SFTP location.



I am thinking of changing the file name directly into the SFTP server through an SSH rename command(but that will be the last option ).


Hi Arka,



Got it so the pre-script is not doing it's job i.e.updating the file name before the export starts.


What I think is the pre-script is actually executed before the file is actually placed at the mid server and not when the export starts i.e. process of extracting the records and forming a csv or xls file etc.


First the attachment with that file name is created and then servicenow must be putting records into that one by one



you can check that in the export history, you should see 3 logs



a) Created an attachment 'incident.csv' on MID Server Attachment table


b) Export started


c) Transferred incident.csv to the MID Server 'Helsinki_MIDServer'



so you should change the filename as soon as step (a) is completed which means once attachment is created on MID Server Attachment table(ecc_agent_attachment)



So I would tell one more approach here.


Have a business rule on table i.e. 'sys_attachment'.



Business rule: After insert BR on table 'sys_attachment' and condition will be table name is 'ecc_agent_attachment'. 'ecc_agent_attachment' table is actually holding the attachment i.e. csv or xls file which will be picked up and exported to mid server



Script: update the file_name for the attachment record to yours as soon as record is inserted



var nowDate = new GlideDateTime().getDate();


var currentFileName = current.file_name;



currentFileName = currentFileName.substring(0, currentFileName.lastIndexOf(".")); // this will give you the text "incident"



var modifiedFileName = currentFileName + '_' + nowDate + '.csv'; // remember to attach the extension for example I have used



current.file_name = modifiedFileName;


current.update();



you can try this and if not achieved then as you mentioned you can directly rename file into the SFTP server through an SSH rename command



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Wow!!! simillar thoughts.... Excatly thinking of the same.



Planning to write a business rule to change the name of the attachment before it attaches and creates a record in ecc_agent_attachment...


Hi Arka,



Any update on this?


Can you mark my answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.



Regards


Ankur


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