Send attachment to SFTP server by using script

Uday Kumar1
Tera Contributor

I am using the below script to send the attachment to SFTP server. Here I have created Integration connection as SSH private key connection which is connected successfully. After running the script in the flow action it is not showing any logs. What are the pre requisites to achieve this functionality. We have Integrationhub plugin. Any examples will be help full. 

 

 

 

 

sapContent += handleNull(newTrail.u_gg_int_record_type) + '|"' +
handleNull(newTrail.u_record_count) + '\n';

gs.info('SAP Content Length: ' + sapContent.length);
if (sapContent.length === 0) {
gs.warn('SAP content is empty, attachment will not be created.');
return;
}

if (headerSysId) {
var fileName = newHeader.u_gg_int_file_name;
var grAttachment = new GlideSysAttachment();
try {
grAttachment.write(newHeader, fileName, 'text/plain', sapContent);
gs.info('Attachment created successfully: ' + fileName);
} catch (e) {
gs.error('Error attaching file: ' + e.message);
}
// SFTP Transfer
var sftp = new GlideSFTP();
var sftpConnection = sftp.getConnection('Integration Connection');
if (sftpConnection) {
try {
var remotePath = 'path' + fileName;
sftpConnection.upload(remotePath, sapContent, 'text/plain');
gs.info('File uploaded successfully to SFTP: ' + remotePath);
} catch (sftpError) {
gs.error('SFTP upload failed: ' + sftpError.message);
}
} else {
gs.error('Failed to establish SFTP connection.');
}
} else {
gs.error('Failed to create HEADR record.');
}
})(current, previous);

1 REPLY 1

Tony Branton
ServiceNow Employee
ServiceNow Employee

Please post this to another forum - this is unrelated to the CMDB.

 

Thanks.