+ Start a Discussion
PraeneiPraenei 

Create csv file from Quick Action - how to close VFP?

Hi.

This shouldn't be difficult but not found a way to solve it.

The user requirement is for a quick action button to appear on one of our custom objects.  If user clicks it then a csv file will be generated containing the email addresses of contacts linked to the custom object.  The problem I have is that after the user clicks the quick action button the csv file is generated but the VFP (blank screen) stays open. I ideally want this to disappear.

Controller (ExportCaseReportEmailsCtrl)
public with sharing class ExportCaseReportEmailsCtrl {
    
    public List<case_contact__c> contactDetails {get; set;}
    
    public ExportCaseReportEmailsCtrl(ApexPages.StandardController controller) {
        getContactDetails();
    }
    
    public PageReference getContactDetails(){
        
        contactDetails = [select id, contact__r.name, contact__r.account.name, contact__r.email 
                                 from case_contact__c 
                                 where Case_Number__c in 
                                 (select case_number__c 
                                  from case_report_Code__c 
                                  where report_code__r.id = :ApexPages.currentPage().getParameters().get('id')
                                 )
                                 and contact__r.email != null];
        return new PageReference('');
    }
    
}
VFP :-
<apex:page standardController="report_code__c" extensions="ExportCaseReportEmailsCtrl" cache="true" contentType="text/csv#reportCodeEmails.csv;charset=utf8" language="en-US">"Name2","Client","Email"
<apex:repeat value="{!contactDetails}" var="a">
"{!a.contact__r.name}","{!a.contact__r.account.name}","{!a.contact__r.email}"
</apex:repeat>
</apex:page>
Screenshot of VFP remaining open
Screenshot of VFPThanks
 
Best Answer chosen by Praenei
PraeneiPraenei
I have a solution.  It may not be the best but rather than havinmg the VFP as the quickaction I've now created a new Lightning Component & the Quick Action refers to that:-

LC component:-
<aura:component implements="force:lightningQuickAction,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute name="recordId" type="Id" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
	
</aura:component>
LC controller :-
({
	doInit : function(component, event, helper) {
        window.open("/apex/ExportCaseReportEmails?id="+component.get("v.recordId"));
        $A.get("e.force:closeQuickAction").fire();
	}
})

The rest of the code remains the same.

Hope it helps someone else in the future.

 

All Answers

AnudeepAnudeep (Salesforce Developers) 
You can make use of javascript to close this

See example here
PraeneiPraenei
Thanks for responding @Anudeep but unfortunately because the requirement is to generate a CSV file, the VFP is described as contentType="text/csv#reportCodeEmails.csv;charset=utf8".  This means that any javascript I put in the VFP appears in the extract rather than being actioned.
PraeneiPraenei
I have a solution.  It may not be the best but rather than havinmg the VFP as the quickaction I've now created a new Lightning Component & the Quick Action refers to that:-

LC component:-
<aura:component implements="force:lightningQuickAction,force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute name="recordId" type="Id" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
	
</aura:component>
LC controller :-
({
	doInit : function(component, event, helper) {
        window.open("/apex/ExportCaseReportEmails?id="+component.get("v.recordId"));
        $A.get("e.force:closeQuickAction").fire();
	}
})

The rest of the code remains the same.

Hope it helps someone else in the future.

 
This was selected as the best answer