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
Ritesh001Ritesh001 

Export CSV using lightning component override column name with Custom names

Using JS i am exporting CSV file, How to override the keys with custom column names?

convertArrayOfObjectsToCSV : function(component,objectRecords){
        // declare variables
       var csvStringResult, counter, keys, columnDivider, lineDivider;
       
        // check if "objectRecords" parameter is null, then return from function
        if (objectRecords == null || !objectRecords.length) {
            return null;
         }
        // store ,[comma] in columnDivider variabel for sparate CSV values and 
        // for start next line use '\n' [new line] in lineDivider varaible  
        columnDivider = ',';
        lineDivider =  '\n';
 
        // in the keys valirable store fields API Names as a key 
        // this labels use in CSV file header
        keys = ['case__r.CaseNumber','Name','Resolved__c','In_Conflict__c','On_Another_Case__c','Payment_Terms__c','Original_Charge__c',
                'Proposed_Adjustment_Amount__c','Proposed_Difference_Amount__c','Initial_Proposed_Difference_Amount__c',
                'Adjusted_Total_Charge__c','Difference_Amount__c','Current_Balance__c','Invoice_Bill_Type__c',
                'Invoice_Correction_Type__c','Service_Level__c','Service__c','Rating_Status__c','Rated_Date__c','Pickup_Date__c'];
       csvStringResult = '';
       csvStringResult += keys.join(columnDivider);
       csvStringResult += lineDivider;
 
        for(var i=0; i < objectRecords.length; i++){   
            counter = 0;
           
             for(var sTempkey in keys) {
                 var skey = keys[sTempkey] ;   
              // add , [comma] after every String value,. [except first]
                  if(counter > 0){ 
                      csvStringResult += columnDivider; 
                   }
                 
                 if(skey == 'Case Number') {
                     csvStringResult += '"'+objectRecords[i].Case__r.CaseNumber+'"';
                 }
                 
                 else if(objectRecords[i][skey] != undefined ){
                     csvStringResult += '"'+ objectRecords[i][skey]+'"';
                 }else{
                     csvStringResult += '"'+ '' +'"';
                 } 
               
               counter++;
 
            } // inner for loop close 
             csvStringResult += lineDivider;
          }// outer main for loop close 
       
       // return the CSV formate String 
        return csvStringResult;        
    },
MagulanDuraipandianMagulanDuraipandian
You can create custom report type. In the report type, you can change field label. Then you can export it.
https://help.salesforce.com/articleView?id=000315838&language=en_US&type=1&mode=1
--
Magulan Duraipandian
www.infallibletechie.com
Rohit M 43Rohit M 43
keys = ['case__r.CaseNumber','Name','Resolved__c','In_Conflict__c','On_Another_Case__c','Payment_Terms__c','Original_Charge__c',
                'Proposed_Adjustment_Amount__c','Proposed_Difference_Amount__c','Initial_Proposed_Difference_Amount__c',
                'Adjusted_Total_Charge__c','Difference_Amount__c','Current_Balance__c','Invoice_Bill_Type__c',
                'Invoice_Correction_Type__c','Service_Level__c','Service__c','Rating_Status__c','Rated_Date__c','Pickup_Date__c'];

var keyLabel = ['Case Number', 'Name', 'Resolved', 'In Conflict','On Another Case','Payment Terms','Original Charge','Proposed Adjustment Amount','Proposed Difference Amount','Adjusted Total Charge','Difference Amount',.............];
        csvStringResult='';
        csvStringResult+=keyLabel.join(columnDivider);

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.
Thanks,
Rohit