function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
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=9062I000000QxiLQAS2. 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
You can use apex code to export the record in csv file :-
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