0% found this document useful (0 votes)
254 views

SFTP - Apex

This Apex code example demonstrates how to directly integrate Salesforce with an SFTP server without using an external client. The code defines an SFTPIntegration class with a transferFile method that uses the HttpCalloutMock interface to make an authenticated GET request to the SFTP server and retrieve a remote file. If successful, the file content is returned as a Blob and can be inserted as an Attachment in Salesforce.

Uploaded by

R. Ünver Soykam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
254 views

SFTP - Apex

This Apex code example demonstrates how to directly integrate Salesforce with an SFTP server without using an external client. The code defines an SFTPIntegration class with a transferFile method that uses the HttpCalloutMock interface to make an authenticated GET request to the SFTP server and retrieve a remote file. If successful, the file content is returned as a Blob and can be inserted as an Attachment in Salesforce.

Uploaded by

R. Ünver Soykam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chat GPT Question:

can you give me an example for apex code integration (without using any other client, directly
between Salesforce and SFTP)

ANSWER
public class SFTPIntegration {

public static void transferFile() {

String username = 'your_username';

String password = 'your_password';

String server = 'sftp.yourserver.com';

Integer port = 22;

String path = '/path/to/remote/file.txt';

String localPath = 'file.txt';


String ftpCommand = 'GET ' + path;

HttpRequest req = new HttpRequest();

req.setMethod('GET');

req.setEndpoint('callout:SFTP/' + server + ':' + port);

req.setHeader('Content-Type', 'text/plain');

req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' +


password)));

req.setBody(ftpCommand);

req.setTimeout(120000);

Http http = new Http();

HttpResponse res = http.send(req);

if (res.getStatusCode() != 200) {

System.debug('Failed to transfer file. Status code: ' + res.getStatusCode() + ', message: ' +
res.getStatus());

} else {

Blob fileContent = res.getBodyAsBlob();

String fileAsString = fileContent.toString();

// do something with the file content, such as insert into a custom object

Attachment attachment = new Attachment();

attachment.Name = 'file.txt';

attachment.Body = fileContent;

attachment.ParentId = 'your_record_id';

insert attachment;

}
SFTPIntegration.transferFile();
public class SFTPIntegration {

public static void transferFile() {

String username = 'your_username';

String password = 'your_password';

String server = 'sftp.yourserver.com';

Integer port = 22;

String path = '/path/to/remote/file.txt';


String localPath = 'file.txt';

String ftpCommand = 'GET ' + path;

HttpRequest req = new HttpRequest();

req.setMethod('GET');

req.setEndpoint('https://' + server + ':' + port);

req.setHeader('Content-Type', 'text/plain');

req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(username + ':' +


password)));

req.setBody(ftpCommand);

req.setTimeout(120000);

Http http = new Http();

HttpResponse res = http.send(req);

if (res.getStatusCode() != 200) {

System.debug('Failed to transfer file. Status code: ' + res.getStatusCode() + ', message: ' +
res.getStatus());

} else {

Blob fileContent = res.getBodyAsBlob();

String fileAsString = fileContent.toString();

// do something with the file content, such as insert into a custom object

Attachment attachment = new Attachment();

attachment.Name = 'file.txt';

attachment.Body = fileContent;

attachment.ParentId = 'your_record_id';

insert attachment;

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy