简体   繁体   中英

How to pass the csv file from file cabinet in NetSuite to ftp server using JavaScript?

Could you help me do the task using JavaScript?

I have a task and if i do it manually it looks like this:

  1. i create Saved Search in NetSuite.
  2. Download the result of created saved search in csv.
  3. The i put this file on ftp server, using FileZilla. (i had a connection with server previously: write a domain, username and password - that's all)

Now, a need it solve through sutlet script. 1. Create Saved Search - done 2. Create csv with result of saved search in content and put it in file cabinet in the NetSuite - done 3. Ok, now i have a needs me file but i do not understand how to pass it on ftp.

*i tried to study several articles, but frankly speaking could not to solve my problem. Moreover, their article seems about manually method not automative

this aritcle - https://ursuscode.com/netsuite-tips/suitescript-2-0-sftp-tool/ *

var searchResult = Contacts.run().getRange(0,999);
                log.debug('Contacts', searchResult);
                var Header = 'INTERNAL ID' + ';' + 'FIRST NAME' + ';' + 'LAST NAME';
                var Content = "";

                for (var i = 0; i < searchResult.length; i++) {

                    var internalid = searchResult[i].getValue('internalid');
                    var FirstName = searchResult[i].getValue('firstname');
                    var LastName = searchResult[i].getValue('lastname');

                    Content = Content + internalid + ';' 
                                + FirstName + ';'
                                + LastName; 
                    Content = Content + '\n';
                }

                var fileObj = file.create({
                    name: 'test.csv',
                    fileType: file.Type.CSV,
                    contents: Header + '\n' + Content
                    });
                    fileObj.folder = 45434;
                    var fileId = fileObj.save();

                    var savedFileObj = file.load({
                        id: fileId
                    });

                    var myPwGuid = '';
                    var myHostKey = ''
                    var objConnection = sftp.createConnection({
                        username: '',
                        passwordGuid: myPwGuid,
                        url: 'ftp.expertsender.com',
                        hostKey: myHostKey
                        });

NetSuite不支持ftp,它仅支持sftp。

NetSuite has the support for SFTP, FTP and SFTP runs in different port numbers, However FTP transfers data in plain text format which will compromise your security, using SFTP is the better option as it will transfer your data in encrypted format and security is assured.

In your example, I believe you're calling FTP request which will not work in this case.

Oki,

Now, the article you did mention is the right one : why ? Because the first required step to be able to use SFTP is to generate a GUID. You are talking about manual methods, well yes, including the one in that Article, but it is not a problem, because once you have generated the GUID, you don't need to change it, so it is a one time action, unless your ftp credential change.

So, first step : use "ursuscode" to create a Suitelet. Deploy that suitelet and use it to generate the GUID (it is a form where you need to set the ftp password, host...). Using the same form, you can then generate the HOST key (check the video).

Second step, use the Generated GUID and HOST Key in your code.

Third step, add the code to upload the file : from netsuite help page, here is an example:

connection.upload({
            directory: 'relative/path/to/remote/dir',
            filename: 'newFileNameOnServer.js',
            file: myFileToUpload,
            replaceExisting: true
        });

By the way, you can upload the file without the need to Save and Reload it again ( https://system.na2.netsuite.com/app/help/helpcenter.nl?fid=section_4617004932.html ).

Note: remember that this is an SFTP, so probably support only SFTP not FTP.

Suggestion: About the GUID (and the other data needed for the connection), I suggest that you use a Script Parameter to provide the GUID to your script code, so if your password change, you can regenerate the GUID and update the script parameter value without the need to touch your code.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM