function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jeff Bryant 16Jeff Bryant 16 

Generate csv file from list of exported users daily

Is it possible (and if so, could I get some examples) of running a daily batch job to export a list of all users in the system to a csv file?
VinayVinay (Salesforce Developers) 
Hi Jeff,

Generate a Report and schedule a batch to export the report into CSV format.

Sample code:
global class export implements System.Schedulable {
    global void execute(SchedulableContext sc) {
        ApexPages.PageReference report = new ApexPages.PageReference('/00O500000000000?csv=1');
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('report.csv');
        attachment.setBody(report.getContent());
        attachment.setContentType('text/csv');
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
        message.setSubject('Report');
        message.setPlainTextBody('The report is attached.');
        message.setToAddresses( new String[] { 'asdf@asdf.com' } );
        Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
        
    }
}

References:
https://douglascayers.com/2016/03/20/salesforce-easy-ways-to-export-data-as-csv/
https://salesforce.stackexchange.com/questions/126771/exporting-salesforce-record-dynamically-using-data-loader

Hope this helps...

Thanks,