Friday, 31 May 2019 15:44

NetSuite's SFTP Module - N/sftp

Written by

With NetSuite's SuiteScript 2.0 you have the ability to access ssh servers, commonly known as secure FTP or SFTP.

You can upload or download files from an SFTP server. It is not possible to send files to NetSuite using the SFTP module. If you are looking for that, you can use a RESTLet.

If you are sure, SFTP is the answer to your question, we have a little bit of help for you here:

 

1) Alta Via has developed many SFTP integrations. Contact us if you like us to help you.

 

2) If you want to do this all by yourself, here are four steps you need to follow for a basic setup or test:

 

2.1) What do you need? Your checklist:

  • URL Address (host)
  • Port
  • Folder Name
  • Password
  • Host Key
  • Host Key type (one of these three: dsa, ecdsa, rsa )
  • GUID (Globally Unique Identifier)

While URL, Port, Folder name and password are relatively easy to get (just ask the administrator of the service), you might not easily get the host key.

 

2.2) Host Key

In a Windows command shell or on a Mac in terminal, run this command:

ssh-keyscan -t rsa -p [port] [host]

 

2.3) GUID

This is a unique identifier which the SFTP Module needs to create a connection with the remote server. Just copy the script below and enter the internal ID of the script which will later handle the connection and the domain. (restrictToScriptIds and restrictToDomains). Deploy it as a SuiteLet and run it through the link you find in the deployment.

 

/**
 *@NApiVersion 2.x
 *@NScriptType Suitelet 
 */

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_internal_id',
                    restrictToDomains: 'host of service'
                });
                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
        };
    }
);

 

2.4) Connection and transfer

The second script handles sending your file to the remote server. This is a workflow action script. You can modify it to be a module and pass sftp credentials and a file.

/**
 *@NApiVersion 2.x
 *@NScriptType WorkflowActionScript
 */

define(['N/sftp', 'N/file', 'N/search'],
    function (sftp, file, search) {
        function onAction(context) {

            var txn = context.newRecord;
            try {
                
                var myPwdGuid = "WHAT YOUR SUITELET CREATED";
                var myHostKey = "WHAT YOU FOUND FROM SSH-KEYSCAN";
                var connection = sftp.createConnection({
                    username: 'YOUR USERNAME',
                    passwordGuid: myPwdGuid,
                    url: 'SERVICE DOMAIN',
                    port: PORT OF SERVICE IS USUALLY 22,
                    directory: 'FOLDER',
                    hostKey: myHostKey
                });

                var uploadFile = file.load({
                    id: 'PATH TO SOME FILE'
                });

                connection.upload({
                    filename: fileName,
                    file: uploadFile,
                    replaceExisting: true
                });
            }
        }

        return {
            onAction: onAction
        }
    }
);
Read 2072 times Last modified on Monday, 03 June 2019 10:32

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

CONTACT US!

O NetSuite SuiteCloudDeveloperNetwork rgb 2NetSuite SolutionProvider horiz rgb

NOTE! This site uses cookies and similar technologies.

If you not change browser settings, you agree to it.

I understand