Pushing .csv files to sftp server in scoped application

Nawazulla Shari
Kilo Explorer

Currently in scoped application i am trying to push .csv files to sftp server. I also know that i would need a mid server for the purpose. I have created necessary export set schedule to push the files to mid server which is working fine. Also i know that i would need post script  to push the files to sftp server using JavascriptProbe after export set schedule is ran. I also have the mid server script include  for pushing csv files to sftp server. But somehow the post script written is not calling the mid server script include.

The above scenarios are working fine in my personal developer instance i,e. .csv files gets stored on mid server and pushed to sftp server. Is the problem with JavascriptProbe in scoped application which is failing to call the mid server script include?

Please can someone help me out on this. Below are the required scripts being used  -

 

Mid server script include  - 

 

var SftpFile = Class.create();
SftpFile.prototype = {
initialize: function() {

this.Properties = Packages.java.util.Properties;
this.StringUtil = Packages.com.glide.util.StringUtil;
this.BufferedWriter = Packages.java.io.BufferedWriter;
this.File = Packages.java.io.File;
this.FileWriter = Packages.java.io.FileWriter;
this.Encrypter = new Packages.com.glide.util.Encrypter();
this.FileOutputStream = Packages.java.io.FileOutputStream;
this.FileInputStream = Packages.java.io.FileInputStream;
this.BufferedReader = Packages.java.io.BufferedReader;
this.InputStreamReader = Packages.java.io.InputStreamReader;
this.OutputStraem = Packages.java.io.OutputStream;
this.BufferedOutputStream = Packages.java.io.BufferedOutputStream;
this.Thread = Packages.java.lang.Thread;

this.targetServer = 'server.com';
this.targetPort =22 ;
this.targetUsername = 'test';
this.targetPassword = 'testpassword';
this.deleteAfterUpload= probe.getParameter("deleteAfterUpload");
this.targetPath = probe.getParameter("targetPath");
this.MIDSERVER_FILE_PATH = probe.getParameter("midServerTempPath");
this.MIDSERVER_FILE_NAME = probe.getParameter("fileName");
},

FileTransfer: function() {

try {
var localFileName = this.MIDSERVER_FILE_PATH + "/" + this.MIDSERVER_FILE_NAME;
this.log("Copying from local file of MID Server: " + localFileName);
this.sftpFile(this.targetServer, this.targetUsername, this.targetPassword, localFileName, this.targetPath + this.MIDSERVER_FILE_NAME);
} catch (e) {
this.log("Error in writing file to SFTP server: " + e);
}

},


sftpFile : function(hostName, userName, password, localFileName, remoteFileName) {

this.log('sftpFile(): attempting to connect to ' + hostName);
var ssh = new Packages.com.sshtools.j2ssh.SshClient();
var ignoreHost = new Packages.com.sshtools.j2ssh.transport.IgnoreHostKeyVerification();
if (!this.targetPort){
this.targetPort = 22;
}
this.log('sftpFile(): attempting to connect to ' + hostName + " on port " + this.targetPort);
ssh.connect(hostName, this.targetPort, ignoreHost);

pwd = new Packages.com.sshtools.j2ssh.authentication.PasswordAuthenticationClient();
var authPassword = new Packages.com.glide.util.Encrypter().decrypt(password);
pwd.setUsername(userName);
pwd.setPassword(authPassword);

// Get full path of filename

this.log('sftpFile(): attempting to copy ' + localFileName + ' to ' + remoteFileName);
if(ssh.authenticate(pwd) == new Packages.com.sshtools.j2ssh.authentication.AuthenticationProtocolState().COMPLETE) {
sftp = ssh.openSftpClient();

try {
sftp.put(localFileName, remoteFileName);
this.log("File successfully copied to targert path\n\n");

if (this.deleteAfterUpload == "true") {
this.log("deleteAfterUpload -> " + this.deleteAfterUpload + ", deleting local file...");
new this.File(localFileName)["delete"]();
}

}

catch(e) {
this.log('FILE NOT FOUND ' + remoteFileName + ' or error: ' + e);
}
sftp.quit();
try{
// kill connection
ssh.disconnect();
}
catch(e){
this.log('Manual connection kill not successful with error: ' + e);
}
}

},

log: function(data) {
ms.log(data);
},

};

 

Post script of export set schedule - 

var jprobe = new JavascriptProbe("SN_MIDserver_UKSubDev2");
jprobe.setName("Sftp File"); //any name can be given
jprobe.setJavascript("new Test().FileTransfer();");
jprobe.addParameter("targetPath",'test/testfolder');
jprobe.addParameter("fileName", "article_export.csv");
jprobe.addParameter("midServerTempPath", "D:/MIDserver/SN_MIDserver_UKSubDev2");
jprobe.addParameter("deleteAfterUpload", true);
jprobe.create();

 

6 REPLIES 6

Hi Shariff,

Did you check mid server is up and running and is validated?

Also don't include any line just include single line in FileTransfer method

ms.log("MID Server Script Called");

Grab the log file from mid server and check whether this line is coming or not

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,

 

Yes my mid server is up and running. Also i have checked to find out that after running export set schedule the .csv files do get stored on mid server path.

I Also  removed everything from mid server script include expect file transfer function, but still no logs are generated when post script runs.