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
Krishnan MishraKrishnan Mishra 

Why my CSV file is blank?

I try to pass on a list to a new visualForce page and then display that list as a table to download as a csv file. But I am getting blank csv file. As soon as i redirect to my new page the list gets empty even though "setRedirect=false". Following is my code:
Controller code:
public class PaginationForComponent {
  public PaginationForComponent(){
    allContactList = new list<wrapper>();
    myOrder = 'desc';
    sortField='name';
    PageNumber = 1;
    alphabet = new list<string>{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Others','All'};
    RecordsPerPageslist = 10;
    System.debug('constructor called');
  }
  Boolean lstNamePresent=false;
  public String objName{get;set;}
  public String fieldNames{get;set;}
  list<sObject> con = new list<sObject>();
  public String alphaSearchConct{get;set;}
  public string msg {get;set;}
  public Map<id,Boolean> m = new Map<id,boolean>(); 
  list<sObject> sortedList;
  public String myOrder{get;set;}                 // Ascending or Descending order of sorting
  public String sortField{get;set;}               // Field by which sorting should be done
  public boolean selectAll{get;set;}
  public list<String> alphabet{get;set;}
  public list<sObject> cont;
  public list<wrapper> allContactList{get;set;}
  public list<wrapper> ct = new list<wrapper>();
  public list<wrapper> csvList = new list<wrapper>();
  public list<String> query = new list<String>();
  public list<String> csvQuery;
public Pagereference submit() {
    csvList = new list<wrapper>();
    csvQuery = new list<String>();
    try {
    for (id i: m.keySet()) {
      System.debug('id is '+i);
      if (m.get(i) == true) {
        sObject c = Database.query('SELECT ' + fieldNames + ' FROM ' + objName + ' WHERE id= :i');
        ct.add(new wrapper(c));
        System.debug('ct is '+ ct);
      }
     }
    } 
    catch (System.QueryException e) {
     System.debug('Error while generating csv file ' + e);
    }
    csvList.addAll(ct);
    csvQuery.addAll(query);
    System.debug('csv list earlier ' + csvList);
    Pagereference pgReference = Page.ConvertToCSVController;
    PgReference.setRedirect(false);
    return pgReference;
    }
public list < wrapper > getgenerateCSV() {
    System.debug('csv list is :' + csvList);
    return csvList;
  }
  public String[] getqueries(){
    return csvQuery;
  }
    public class wrapper{
      public boolean isSelected{get;set;}  
      public sObject con{get;set;}  
      public   wrapper(sObject con){
              //System.debug('constructor of wrapper class called ');
              isSelected = false;
              this.con = con;
          }
    }
  }

My  page code(on which it is redirected)
<apex:page controller="PaginationForComponent" contentType="application/vnd.ms-excel#contacts.xls" readOnly="True">
<apex:form id="form">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!generateCSV}" var="csv">
            <apex:repeat value="{!queries}" var="fldNames">
                <apex:column value="{!csv.con[fldNames]}"/> 
            </apex:repeat>
        </apex:pageBlockTable> 
    </apex:pageBlock>
</apex:form>
</apex:page>

 
NagendraNagendra (Salesforce Developers) 
Hi Mishra,

Sorry for this issue you are encountering.

Your Visualforce page does not call the action method that performs work in your class, submit().

The getter methods for the two properties you're iterating over, generateCSV and queries, both simply return the data that's stored in instance variables - they don't query the database or perform any work.
Since submit() is never called, both methods will simply return the empty lists that are created when your controller is instantiated, which you should see in your debug logs for generateCSV().

You need to refactor your code to pull the CSV-generation logic out of submit() into a non-action method, say generateData(). Then, call that method from your class constructor to ensure the data is populated.

However, I'll also note that you don't appear to be populating fieldNames anywhere, so you'll get a QueryException.

Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra