I have to write an apex batch scheduler to send user information csv to some ftp server. I am able to generate the csv file with user records but I don't know how to send this csv file to ftp server with using the code.

Do we have any api methods to send generated csv to the ftp server.. by providing the username and password?

public class UserInformation implements Database.Batchable<SObject>, Database.Stateful {
    string[] Lines;     ///  all records

    public Database.QueryLocator start(Database.BatchableContext bc) {
        lines = new String[0];
        String query = 'SELECT Username,Email from USER';
        return Database.getQueryLocator(query);

    public void execute(Database.BatchableContext bc, SObject[] scope) {
        // process
        for (Sobject record : scope) {
            String line = '';
            // header row
            if (lines.size() == 0) {
                line = 'UserName, Email';
            } else {
                // build your csv line here
                line += '"' + record.get('UserName') + '"'
                        + ',' + '"' + record.get('Email') + '"';

                system.debug('Line No >>> ' + line);
        }  // end of for loop

    public void finish(Database.BatchableContext bc) {
        // this will send the CSV file as an attachment
        String finalResults = String.join(lines, '\n');
        lines = null; // Free temp memory to avoid heap errors
        blob csvBlob = Blob.valueOf(finalResults);

        // how do i send it to ftp server from here now?
  • Not sure how to do it through code, but as an alternative you could use Jitterbit free DataLoader and schedule an export of a csv file to an ftp site.
    – Jenny B
    Dec 10 '14 at 17:17
  • ok thanks.. is that free? and can i add the scheduler in jitterbit also.. and isnt it possible through salesforce data loader? Dec 10 '14 at 17:24
  • Haven't used DataLoader in a long time so I'm not sure, but you can create a schedule in Jitterbit and yup it's free.
    – Jenny B
    Dec 10 '14 at 17:26
  • Take a look at this question/answer, I added screenshots of how to do it... salesforce.stackexchange.com/questions/56898/…
    – Jenny B
    Dec 10 '14 at 17:28
  • Thanks alot! I think this is what i needed... Let me try! Dec 10 '14 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.