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
S Balaji 8S Balaji 8 

I have a requirement where newly inserted or updated data from the custom object has to be automatically exported to a csv file that is present under files object.

PriyaPriya (Salesforce Developers) 
Hi,

You can use apex code to export the record in csv file :-
public class ExportRecords {
    public class ActionInput{
        public String objectApiName;
        public String fileTitle;
        public Boolean addTimeStamp;
        public SObject[] lstRecords;
    }
    public class ActionOutput{
        public String contentDocumentId;
    }
    public static void generateCSV(List<ActionInput> aiList){
        if(aiList.size()>0 && aiList[0].lstRecords.size()>0)
        {
            ContentVersion cv = new ContentVersion(
                Title = aiList[0].fileTitle,
                VersionData = createCSVContent(getFieldAPIMap(aiList[0].objectApiName),aiList[0].lstRecords),
                PathOnClient = aiList[0].fileTitle+'.csv'
            );
            insert cv; 
            System.debug(cv);
        }
    }
    public static Map<String,String> getFieldAPIMap(String objName){
        Map<String,String> fieldAPIMap = new Map<String,String>();
        Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(objName).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
        {
            schema.describefieldresult dfield = sfield.getDescribe();
            fieldAPIMap.put(dfield.getname(),dfield.getLabel());
        }
        System.debug(fieldAPIMap);
        return fieldAPIMap;
    }
    public static Blob createCSVContent(Map<String,String> fieldAPIMap,SObject[] lstRecords){
        String csvStringContent;
        try{            
            String csvHeader = String.join(fieldAPIMap.values(), ',') + '\n';
            csvStringContent = csvHeader;
            List<String> fieldAPINames = new List<String>(fieldAPIMap.keySet());
            Integer recordListSize = lstRecords.size();
            Integer fieldListSize = fieldAPINames.size();
            for(Integer i=0;i<recordListSize;i++){
                SObject sObj = lstRecords[i];
                for(Integer j=0;j<fieldListSize;j++){
                    String fieldAPIName = fieldAPINames[j];
                    Object fieldValue = sObj.get(fieldAPIName);
                    if(fieldValue != null){
                        csvStringContent = csvStringContent + String.valueOf(fieldValue).escapeCsv();
                    }else{
                        csvStringContent = csvStringContent + '';  
                    }
                    csvStringContent += + ',';
                }
                csvStringContent += '\n';
            }
        }catch(Exception e){
            System.debug(e.getMessage() + e.getLineNumber());
        }
        return Blob.valueOf(csvStringContent);
    }
}


Note :- Use apex to build a CSV file and save as an attachment of a record, users can download the attachment.

Reference :- https://developer.salesforce.com/forums/?id=9062I000000QxiLQAS
2. https://www.salesforcetutorial.com/how-to-export-data-in-csv-by-using-apex-code/

If the information was helpful, kindly mark it as the best answer.

Thanks & Regards,

Priya Ranjan