Encountering an Unexpected SuiteScript Error When Uploading a File to SFTP using the script:
function xmlFileCreation(xmlContent, recId, recordType) {
try {
log.debug("recordType**2", recordType)
let nowDate = new Date();
log.debug("nowDate", nowDate);
let xmlFileName = 'CORP_XML_ITEM' + "_" + recId + "_" + nowDate + '.xml';
log.debug("xmlFileName", xmlFileName);
let xmlFile = file.create({
name: modifiedFileName,
fileType: file.Type.XMLDOC,
contents: xmlContent,
description: 'Material xml file.',
encoding: file.Encoding.UTF8,
folder: 398248, //change
isOnline: true
});
let xmlid = xmlFile.save();
setSftpConnection(xmlid, recId, recordType);
} catch (e) {
log.debug("error@xmlFileCreation", e.message)
return false;
}
}
function setSftpConnection(xmlid, recId, recordType) {
//Creating connection to SFTP
let userName = 'jobinandjismi';
let passwordGuid = 'ec8e2cc9ba12497280388c7e22012c14';
let url = 'sftp.corpdesign.com';
let hostKeyString = 'AAAAB3NzaC1yc2EAAAABIwAAAIEAs2E5nCxBe4M6cxCEYus+1UwQv9Mc21JlCmtpPDaS9A2itBuQms0CoRHR5UelKKN01yDr7u1MzVe5efGppchDcpQ0zu5bp/GcUOrj5LWnjxQg/PpGRo7QMOl194rEbVKGUGne0FmKUqTF48Xq69Ju7V0f7xgf++VCKAXicK/pqFc=';
let connection = sftp.createConnection({
username: userName,
passwordGuid: passwordGuid,
url: url,
directory: '/', // sftp folder for uploading xml file.
hostKey: hostKeyString
});
let materialXmlToUpload = file.load({
id: xmlid
});
// Upload the file to the remote server
connection.upload({
directory: '/Material Files', // sftp folder for uploading material xml files
filename: materialXmlToUpload.name,
file: materialXmlToUpload,
replaceExisting: true
});
log.debug("upload successfull")
}
The error was caused by the character “:”(Colon (:)) in the File Name. This can be resolved by removing colon from the file name as:
//replace character ":" from the filename
let modifiedFileName = xmlFileName.replace(/:/g, '.');