Skip to main content
SUJAY GANGULY (Accenture Technology) 님이 #Apex에 글을 올렸습니다

Want create CSV file from APEX

 

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:

 

Want create CSV file from APEX Hi All, Hope you doing well. I am trying create CSV file from apex class.

 

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;

    }

 

}

 

#Apex  #Salesforce Developer  #Triggers  #LWC

 

#Apex  #Salesforce Developer  #Salesforce  #Triggers  #LWC

댓글 1개
  1. 2024년 8월 30일 오전 10:17

    OK While you are creating CSV you must be careful about these things in your data:

    - Comma inside data

    - New line and Line feed values.

    As well as I see you are only assigning   Database.query results as CSV, you should do in loop and control the values inside fields.

     

     SObject[] singleObjDataStorages = Database.query('SELECT ' + singleObjDataStorage.Fields_api_name_of_selected_object__c + ' FROM ' +System.label.SObject_Name );

0/9000