Transfering Files To FTP Server using Suitescript

Transfering Files To FTP Server using Suitescript

Tags :

Category : Uncategorized

In simple terms, FTP is a communication protocol that enables users to share files between systems. We can do this in Netsuite using Suitescript. Following is a sample code that can be used to upload file from Netsuite to the FTP server,

var fileObj = file.load({id: 271});

var FTPurl = ‘test.rebex.net’;

var FTPusername = ‘demo’;

var passwordGuid = ‘7a8812d753a94399996e007f965ac462’;

var hostKey = “AAAAB3NzaC1yc2EAAAABJQAAAQEAkRM6RxDdi3uAGogR3nsQMpmt43X4WnwgMzs8VkwUCqikewxqk4U7EyUSOUeT3CoUNOtywrkNbH83e6/yQgzc3M8i/eDzYtXaNGcKyLfy3Ci6XOwiLLOx1z2AGvvTXln1RXtve+Tn1RTr1BhXVh2cUYbiuVtTWqbEgErT20n4GWD4wv7FhkDbLXNi8DX07F9v7+jH67i0kyGm+E3rE+SaCMRo3zXE6VO+ijcm9HdVxfltQwOYLfuPXM2t5aUSfa96KJcA0I4RCMzA/8Dl9hXGfbWdbD2hK1ZQ1pLvvpNPPyKKjPZcMpOznprbg+jIlsZMWIHt7mq2OJXSdruhRrGzZw==”;

var conn = sftp.createConnection({

‘username’: FTPusername,

‘passwordGuid’: passwordGuid,

‘url’: FTPurl,

‘hostKey’: hostKey

});//Setting up connection

var upload = conn.upload({

directory: ‘/’,

filename: ‘newFileNameOnServer’,

file: fileObj,

replaceExisting: false});//Uploading the file to FTP

Hostkey can be generated using command prompt with the following command:

ssh-keyscan -t rsa -p <port number> <ftp – url>

We need to generate passwordGUID and Hostkey using the following steps:

passwordGUID can be generated using the value from the credential field on a form. For that we need to use form.addCredentialField api. Following is a sample suitelet to get the passwordGUID value.

/**

 *@NApiVersion 2.x

 *@NScriptType Suitelet

 *@NModuleScope Public

 */

define([‘N/ui/serverWidget’,’N/file’,’N/log’,’N/record’,’N/search’,’N/https’],

    function(serverWidget,file,log,record,search,https) {

        function onRequest(context)

   {

            if (context.request.method === ‘GET’)

            {

                  var form = serverWidget.createForm({title: ‘Password Form’});

                  form.addCredentialField({

                    id: ‘password’,

                    label: ‘Password’,

                    restrictToDomains: [‘test.rebex.net’],//your ftp server url comes here

                    restrictToScriptIds: ‘customscript_test_ftp_transfer’//The IDs of the scripts that are allowed to use this credential field.

                  });

    form.addSubmitButton();

    context.response.writePage({pageObject: form});

              }

              else

              {

                  var PostForm = serverWidget.createForm({title: ‘Password Form’});

                  var passwordGuid = context.request.parameters.password;

                  PostForm.addField({

                    id : ‘passwordguidresponse’,

                    type : serverWidget.FieldType.LONGTEXT,

                    label : ‘PasswordGUID Response’,

                    displayType: serverWidget.FieldDisplayType.INLINE

                }).defaultValue = passwordGuid;

                context.response.writePage({pageObject: PostForm});

              }

        }

        return {

            onRequest: onRequest

        };

    });

Following screenshot shows the UI of the above suitelet. We need to enter FTP server Password here.  

On Clicking Save button,Password GUID response will be shown.