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
vivek jagatap 6vivek jagatap 6 

export files using Apex

I have a apex code which imports the data and upload it into the system.
After the upload I want the success records to be downloaded in excel.
David Zhu 🔥David Zhu 🔥
You cannot use apex to export records directly to a folder. But there are workaround without paid apps.
1. Use report to export records in  .CSV or excel format.
2. Use apex to build a CSV file and send CSV as email attachment
3. Use apex to build a CSV file and save as an attachment of a record, users can download the attachment.
4. Use dataloader or salesforce workbench.
varun kumar 212varun kumar 212
You can export a list of records into a CSV file.

Please refer below code
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);
    }
}

 
Anjali Deshmukh 3Anjali Deshmukh 3
@varun Kumar 212 how to execute it from an anonymous window?
Anjali Deshmukh 3Anjali Deshmukh 3
@varun Kumar 212 how to execute it from an anonymous window?