Convert All Accounts as CSV and Send To sFTP
- abhyash
- Nov 4, 2022
- 2 min read
Updated: Nov 5, 2022
V1 Prepared By: Abhyash Timsina – 4 November 2022
Motivation: Say you have a requirement that every quarter, you need to get all Account records with specific fields as CSV and send to a sFTP server to store and backup it. This solution will make that happen. It uses various components including FTP API. Note - This is a personal project and not intended for financial gain.
My Technical Components

Open-Source Technical Components

Custom Settings Configuration

Source Code for SendToFTP Apex Class (with annotation starting with //)
/**
* Created by Abhyash Timsina on 2/11/2022.
*/
// Apex that uses Schedulable context
global with sharing class SendToFTP implements Schedulable {
global void execute(SchedulableContext sc)
{
convertToCSV();
}
// Method to get all account records and convert to csv
public static void convertToCSV(){
// Can add other account fields here in the SOQL - shame we can't add all without specifying each field
String csv = 'Id,Name\n';
for ( List<Account> accts : [SELECT Id, Name FROM Account] ) {
// Formats the csv, adding comma
for ( Account acct : accts ) {
csv += acct.Id + ',' + acct.Name.escapeCsv() + '\n';
}
}
// Create a file in salesforce and store the csv files data
ContentVersion file = new ContentVersion(
Title = 'Accounts Backup - ' + System.now(),
VersionData = Blob.valueOf( csv ),
PathOnClient = '/Accounts Backup - ' + System.now() + '.csv'
);
insert file;
// Call the sendToFTP method with the csv files name and id
sendToFTP(file.Title, file.Id);
}
// Cannot do DML and Callout in same method so have to use future method
@Future (Callout=true)
public static void sendToFTP(String title, Id fileId) {
uploadFiles(title, fileId);
}
// Method that does the actual callout to upload the files
public static FTPResponseWrapper uploadFiles(String title, Id fileId){
List<FileWrapper> lstFileWrapper = new List<FileWrapper>();
// Finds the File again by querying ContentVersion with the file id as parameter
for(ContentVersion oAttachment : [SELECT Id, ContentDocumentId, VersionData FROM ContentVersion WHERE Id=:fileId]){
// Encode the csv file to base 64
lstFileWrapper.add(new FileWrapper(title + '.csv', '/Desktop', EncodingUtil.base64Encode(oAttachment.VersionData)));
}
// Since we are uploading, set the endpoint to 'upload'
String endpoint = FTPWebServiceUtility.FTP_WebServiceEndpoint+'upload';
// Callout with POST method
HttpResponse response = FTPWebService.uploadFileOnFTPServer(endpoint, 'POST', JSON.serialize(lstFileWrapper));
// Return gets us the body of the response. Not really required practically but since this is not a void method, we need to add it. You can do other innovation here like sending an email to a technical contact stating a new backup was generated etc.
return (FTPResponseWrapper)JSON.deserialize(response.getBody(), FTPResponseWrapper.class);
}
}
To Schedule Apex (run this from DEV console):
System.schedule('Accounts CSV Backup To FTP', '0 0 12 1 1/3 ?', new scheduledCron ());
To check status of your scheduled job, go here:
{Your Salesforce Org}/lightning/setup/ScheduledJobs/home
To Generate other CRON expressions, I recommend this one:
Package Link:
If you need help in installing and getting this working – please contact me on LinkedIn
תגובות