/**
* @NApiVersion 2.1
* @NScriptType ScheduledScript
*/
define(['N/runtime', 'N/search', 'N/file', 'N/sftp'],
(runtime, search, file, sftp) => {
const execute = (scriptContext) => {
log.debug('==START==');
try {
// get the sFTP config
const sftpConfig = getsFTPConfig();
const files = getFilesToSend(sftpConfig);
// send files to sftp site
sendTosFTPServer(files, sftpConfig);
// download files from sftp site
downloadFromsFTPServer(sftpConfig);
}
catch(e) {
log.error('Error', JSON.stringify(e));
}
log.debug('==END==');
}
const getFilesToSend = (sftpConfig) => {
let files = [];
let folderSearchObj = search.create({
type: "folder",
filters: [
["internalidnumber","equalto", sftpConfig.toprocessDirectory]
],
columns:
[
search.createColumn({
name: "internalid",
join: "file",
label: "Internal ID"
})
]
});
folderSearchObj.run().each(function(result){
let fileId = result.getValue({
name: "internalid",
join: "file",
label: "Internal ID"
});
files.push(fileId);
return true;
});
log.debug('Files to send',JSON.stringify(files));
return files;
}
const sendTosFTPServer = (files, sftpConfig) => {
const connection = sftp.createConnection({
username: sftpConfig.username,
keyId: sftpConfig.myKey,
url: sftpConfig.url,
directory: sftpConfig.directory,
hostKey: sftpConfig.myHostKey,
port: sftpConfig.port
});
log.debug('connection', "connected");
let fileName = '';
for (let i = 0; i < files.length; i++) {
let fileId = files[i];
errorMessage = '';
let myFileToUpload = file.load({
id: fileId
});
fileName = myFileToUpload.name;
connection.upload({
filename: fileName,
file: myFileToUpload,
replaceExisting: true
});
log.debug('uploaded file', fileName);
if (sftpConfig.completeDirectory) {
// move to complete folder
myFileToUpload.folder = sftpConfig.completeDirectory;
let fileId = myFileToUpload.save();
log.debug('moved file to complete folder');
}
}
}
const downloadFromsFTPServer = (sftpConfig) => {
const connection = sftp.createConnection({
username: sftpConfig.username,
keyId: sftpConfig.myKey,
url: sftpConfig.url,
directory: sftpConfig.directory,
hostKey: sftpConfig.myHostKey,
port: sftpConfig.port
});
log.debug('connection', "connected");
// get the list of files in the directory
const files = connection.list({
path: ''
});
// download each file
for (let i = 0; i < files.length; i++) {
const downloadedFile = connection.download({
directory: '',
filename: files[i].name
});
downloadedFile.folder = sftpConfig.completeDirectory;
downloadedFile.save();
log.debug('saved file to complete folder', files[i].name);
}
}
const getsFTPConfig = () => {
// use a parameter to get the internal ID of the sFTP configuration record to use
const scriptObj = runtime.getCurrentScript();
const serverConfigId = scriptObj.getParameter({
name: 'custscript_config_id'
});
log.debug('serverConfigId=', serverConfigId);
const conn = search.lookupFields({
type: 'customrecord_sftp_configuration',
id: serverConfigId,
columns: [
'custrecord_sftp_config_user',
'custrecord_sftp_config_key',
'custrecord_sftp_config_url',
'custrecord_sftp_config_port',
'custrecord_sftp_config_host_key',
'custrecord_sftp_config_directory',
'custrecord_sftp_config_to_process',
'custrecord_sftp_config_complete'
]
});
let sftpConfig = {};
sftpConfig.myKey = conn.custrecord_sftp_config_key;
sftpConfig.myHostKey = conn.custrecord_sftp_config_host_key;
sftpConfig.username = conn.custrecord_sftp_config_user;
sftpConfig.port = conn.custrecord_sftp_port;
sftpConfig.url = conn.custrecord_sftp_config_url;
sftpConfig.directory = conn.custrecord_sftp_config_directory;
sftpConfig.toprocessDirectory = conn.custrecord_sftp_config_to_process;
sftpConfig.completeDirectory = conn.custrecord_sftp_config_complete;
log.debug('username=', sftpConfig.username);
log.debug('port=', sftpConfig.port);
log.debug('url=', sftpConfig.url);
log.debug('myKey=', sftpConfig.myKey);
log.debug('myHostKey=', sftpConfig.myHostKey);
log.debug('directory=', sftpConfig.directory);
log.debug('toprocessDirectory=', sftpConfig.toprocessDirectory);
log.debug('completeDirectory=', sftpConfig.completeDirectory);
return sftpConfig;
}
return {execute}
});
Jaime Requena is a seasoned NetSuite Consultant and Solutions Architect, known for delivering WHITE GLOVE service to businesses. With 15+ years of experience and 3x certifications in ERP, Developer, and Admin, Jaime specializes in highly customized NetSuite accounts, transforming operations for 200+ satisfied customers all across the globe.
We aim to bring unmatched expertise and professionalism to your NetSuite initiatives. Let’s talk about how our NetSuite consultancy can make a difference!
It’s Been 4+ Years Now And We Have Worked With Hundreds Of Clients, Building Our Way To The Top, One Happy Client After Another! Their Voices Of Satisfaction Serve As A Testament To Our Success –