Hi All,
Hope you doing well. I am trying create CSV file from apex class. Where i am getting the object name from custom label and depend on the label i query all the fields of that object and then store the object API name and all the fields in Single Object Data Storage custom object and after that depends on the fields trying create a CSV file and try to store that csv file in Single Object Data Storage custom object attachment.
Example: in Custom Label having "Account" api name, so when i run the class, it query all the fields of account and create a record in Single Object Data Storage custom object with "Account" name and the account fields. And after using that query fields i am creating the CSV and store in the same Single Object Data Storage custom object record.
As of now, everything working fine, i can able to create the CSV file and store it in Single Object Data Storage custom object, but the CSV file data are not coming proper way. I understand where is the issue but can't find a way to fix it. If anyone can help?
Here is excel screenshot i am getting:
Below is my code:
public class SingleObjectDataStoreCls {
public static void SingleObjectDataStoreMtd() {
ID singleObjDataStorageID;
List<String> NameFiled = new List<String>();
Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
//Custom label for SObjcet api name: System.label.SObject_Name
Schema.SObjectType sobjType = gd.get(System.label.SObject_Name);
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String, Schema.SObjectField> MapofField = r.fields.getMap();
for(String fieldName : MapofField.keySet())
{
Schema.SObjectField field = MapofField.get(fieldName);
Schema.DescribeFieldResult F = field.getDescribe();
NameFiled.add(F.getName());
}
System.debug('Field Name:***2 '+NameFiled);
String allstring = String.join(new List<String>(NameFiled), ', ');
System.debug(allstring);
//Single Object Data Storage custom object
Single_Object_Data_Storage__c singleObjDataStorage = new Single_Object_Data_Storage__c();
singleObjDataStorage.Fields_api_name_of_selected_object__c= allstring; //adding object fileds from query
singleObjDataStorage.Object_API_name_of_selected_object__c = System.label.SObject_Name; //adding object name from query
insert singleObjDataStorage;
System.debug('Sujay1 singleObjDataStorage '+singleObjDataStorage);
singleObjDataStorageID = singleObjDataStorage.Id;
System.debug('Sujay1 singleObjDataStorageID '+singleObjDataStorageID);
//get fields values
SObject[] singleObjDataStorages = Database.query('SELECT ' + singleObjDataStorage.Fields_api_name_of_selected_object__c + ' FROM ' +System.label.SObject_Name );
//create CSV
String title = System.label.SObject_Name;
String csvFile;
String csvColumnHeader = singleObjDataStorage.Fields_api_name_of_selected_object__c+', Type\n';
List<String> csvRowValues = new List<String>();
csvFile = csvColumnHeader + String.join(singleObjDataStorages,'\n');
title += '.csv';
createFile(csvFile, title, singleObjDataStorageID);
}
public static void createFile(String csvFile, String title, Id singleObjDataStorageID) {
ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.Title = title;
cv.PathOnClient = title;
cv.VersionData = Blob.valueOf(csvFile); //file data
insert cv;
Attachment attachment = new Attachment();
attachment.ParentId = 'a04J2000009p0aBIAQ';
attachment.Name = cv.PathOnClient;
attachment.Body = cv.VersionData;
insert attachment;
}
}
@SUJAY GANGULY That is not the code that I have provided (quickly generated with Perplexity AI). Your code is not correct because you don't manage correctly the delimiters as I said several times.
This problem is already solved with a solution provided here above (generated by AI).
A CSV file must follow this grammar: https://github.com/antlr/grammars-v4/blob/master/csv/CSV.g4
Why don't you follow all the rules of this grammar?
Where is the escaping char in your code (") ?