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
Hitesh Khatri 13Hitesh Khatri 13 

Redirection to previous page after generating CSV from Record List.

Hi All,

 

I am generating a CSV from standard record list for Invoice. When I swith to "All Invoice" from "Recent View" , a Export button appears beside Import button , then I will select the record which I want to export. I have developed a cistom visual force page for generating CSV and it is generating the CSV as I want but the problem I am facing when I click on Export button, It will open the page and redirect me to a blank screen instead of redirecting me back to the list view.

Please help me.

Thanks in advance.

Regards

Hitesh Khatri

Hitesh Khatri 13Hitesh Khatri 13
Or is there any workaround for making custom list button which open the page in new tab?
Hitesh Khatri 13Hitesh Khatri 13
Here is my code :- for the controller
public class CSVGenerator{
    
  
    
    public String csvString{
        get;
        set;
    } 
    
    private ApexPages.StandardSetController standardController;
    
    public CSVGenerator(ApexPages.StandardSetController controller) {
        this.standardController = controller;
    }
    
    @TestVisible
    public CSVGenerator() {
    }
    
    //Method for fetching the selected contacts.
    public PageReference selectedInvoices(){       
        //Get the selected projects
        List<Invoice__c> selectedProjects = (List<Invoice__c>) standardController.getSelected();
        
        // if list is empty then redirect to the all projects page.
        if(selectedProjects.isEmpty()){
            return redirectToPreviousPage(); 
        }else{
            generate(selectedProjects);
            return null ;
        }
    }
    
    @TestVisible
    private void generate( List<Invoice__c> records) {
        Set<Id> invoiceIds = (new Map<Id,SObject>(records)).keySet();
        
        List<Invoice__c> invoices = [SELECT Id, 
                                     Name, 
                                     Account__r.Name ,
                                     Invoice_Date__c, Status__c 
                                     From  Invoice__c Where Id IN: invoiceIds];
        
        String s = line(new String[] {
            format('Invoice Number'),
                format('Account'),
                format('Invoice Date'),
                format('Status')
                });
        for (Invoice__c c : invoices) {
            s += line(new String[] {
                format(c.Name),
                    format(c.Account__r.Name),
                    format(c.Invoice_Date__c),
                    format(c.Status__c)
                    });
        }
        
        csvString = s;
     
        
    }
    
    @TestVisible
    private String line(String[] cells) {
        return String.join(cells, ',') + '\n';
    }
    
    @TestVisible
    private String format(Date d)    {
        return d != null ? '"' + d.format().escapeCsv() + '"' : '""';
    }
    
    @TestVisible
    private String format(String s) {
        return s != null ? '"' + s.escapeCsv() + '"' : '""';
    }
    
    @TestVisible
    private String format(Decimal d, Integer scale) {
        return d != null ? String.valueOf(d.setScale(scale)) : '';
    }
    
    @TestVisible
    private String format(Decimal d) {
        return format(d, 2);
    }
    
    private PageReference redirectToPreviousPage(){
        Schema.DescribeSObjectResult result = Invoice__c.SObjectType.getDescribe(); 
        PageReference pageRef = new PageReference('/' + result.getKeyPrefix()+'?fcf=00B7F000005DUlIUAW'); 
        pageRef.setRedirect(true); 
        return pageRef;
    }
}

And below is the code for my VF page.

<apex:page standardController="Invoice__c" 
           showheader="false"
           contentType="text/csv#invoices.csv"
           extensions="CSVGenerator"
           recordSetVar="invoices"
           action="{!selectedInvoices}"
           cache="true">
        {!csvString}
</apex:page>

It is generating the CSV as per my requirement but I click on the button the page is open in the same tab and I see a blank screen after downling the CSV file. Not able to redirect back to the Invoice Listing.

Please help me out.

 

Regrds

Hitesh Khatri

Keerthana V 1Keerthana V 1
Hi Hitesh,

                 I have a same issue. Did you get resolved this. If yes, please let me know the solution.
Thanks
Keerthana
eswari kondurueswari konduru
Hi 
I have the same issue. Have you solved this issue? If yes, please let me know the solution. Thanks in advance.