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

Raj68
Mega Guru

Hi Nawaz,

Hope below link will help you in your requirement:

https://community.servicenow.com/community?id=community_question&sys_id=30480baddb1cdbc01dcaf3231f961964&view_source=searchResult

NOTE: Mark correct or helpful if it helps you.

Warm Regards,

Raj patel

 

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shariff,

The script include JavascriptProbe check whether the Accessible from is set to "All application scopes"; set that and try once

var jprobe = new global.JavascriptProbe("SN_MIDserver_UKSubDev2");

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

Hello Ankur,

 

My JavaScriptProbe script inlcude is accessible from all the applications. Also i have made necessary changes which you mentioned i,e., var jprobe = new global.JavascriptProbe("SN_MIDserver_UKSubDev2");. But still it does not call mid server script include. I have been checking logs of mid server it does not print anything related to my mid server script. It does not even log about connecting to sftp server.

Below is the screenshot of JavaScriptProbe in my scoped application.

find_real_file.png

 

Do you have any idea why Javascriptprobe is not calling mid server script include. Please help me out on this.

 

The problem is when i run my schedule export the script written in Execute post-export script is not calling your Javascriptprobe that is one thing. So i tried running it using background script , it calls the javascriptprobe as i have put log into it but does not call the mid server script include.