NetSuite SFTP Module

A recent feature introduced in SuiteScript API 2016.2 is the ability to connect to SFTP servers directly from NetSuite. In SuiteScript API 1.0 you had to use a middleware server for an FTP connection, which not only caused a dependency on middleware but was also time consuming and costly. The new SFTP module in SuiteScript API 2016.2 eliminates these issues. However, it does have some shortcomings of its own, such as:

  • There is no way to list the contents of a remote directory
  • The NetSuite SFTP module does not support all types of servers, as NetSuite requires a supported server for encryption algorithms.
  • It does not allow you to connect to FTP or FTPS.
  • There is no way to obtain the server’s host key natively (in the NetSuite environment).

NetSuite’s approved algorithms for connectivity are AES256-ctr, AES192-ctr, and AES128-ctr. The SFTP server can therefore use the following encryption algorithms:

  • AES 128-CTR
  • AES 192-CTR
  • AES 256-CTR

Using the SFTP module in NetSuite

In order to connect to a SFTP server using the SFTP module, here’s what you need to do.

  • Generate the host-key of your SFTP server.
  • Generate GUID in the NetSuite environment.
  • Create a connection to your server.
  • Upload and download the files required

Let’s look at each of these steps in detail

Generate the host-key of your SFTP server

You can easily get your SFTP server’s host key by using the following command.

 Ssh-keyscan hostname or IP address

Generate GUID in NetSuite environment

GUID stands for Global Unique Identifier. This is a unique identifier generated in NetSuite by a particular algorithm. You can use this algorithm to generate a GUID for a specific domain and script.

Let’s look at an example with the help of a sample code snippet for generating a GUID.

define(['N/ui/serverWidget',N/log"],
function (serverWidget, log) {
function onRequest(context) {
if (context.request.method === 'GET') {
var form = serverWidget.createForm({
title: 'Guid Form’
});
form.addField({
id: 'username',
type: serverWidget.FieldType.TEXT,
label: 'Username'
});
form.addCredentialField({
id: 'password',
label: 'Password',
restrictToScriptIds: 'customscript_id, //id of Script Using sftp
restrictToDomains: ‘domain name'
});
form.addSubmitButton({
label: 'Submit Button'
});
context.response.writePage(form);
return;
} else {
var requset = context.request;
var myPwdGuid = requset.parameters.password;
log.debug("myPwdGuid", myPwdGuid);
context.response.write(myPwdGuid);
}
}

return {
onRequest: onRequest
};
});

Create Connection To Your Server

Next, you’ll need to create a connection in the same GUID generator script or in a separate script.
Below is the sample code for that.


function mainFunc(context) {
var myHostKey = "Your Host-Name”;
var connection = sftp.createConnection({
username: 'root',
passwordGuid: "Guid generates from suitelet”,
url: 'sample url’,
directory: '/',
hostKey: myHostKey,
port: 22
});

Next, you’ll need to load the SFTP module to connect to a remote FTP server via SFTP and transfer files.

Upload And Download File

You can create a file to upload to the path of the server by using the function below.

var myFileToUpload = file.create({
name: 'file.txt',
fileType: file.Type.PLAINTEXT,
contents: 'This is uploaded using sftp API in SuiteScript 2.0'
});

connection.upload({
directory: '/',
filename: file.txt',
file: myFileToUpload,
replaceExisting: true
});

You can also download a file from the directory of the server to the file cabinet in NetSuite by using the following function.


var downloadedFile = connection.download({
directory: '/',
filename: file.txt'
});
downloadedFile.folder = folder id in which you want to place your file in file cabinet;
var fileId = downloadedFile.save();

Hope you found this post useful.

 To learn more about how to get the most out of your NetSuite investment, contact us for a free consultation

It's only fair to share...Tweet about this on TwitterShare on LinkedInShare on Google+Share on Facebook
Why A Mobile-First Strategy Matters for Your Ecommerce Store
How To Export NetSuite Contacts to Microsoft Outlook

Netsuite User

Leave a Reply

Your email address will not be published. Required fields are marked *

4 + seventeen =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ContactUs.com