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
SS KarthickSS Karthick 

Rerender a page when action method using pageReference

Hi folks,
    Can anyone tell me how to rerender a particular pageblock when the action method using pageReference?

My use case is I want to rerender a particular block when I click the command button

VFP:
<apex:commandButton action="{!Download}" value="download" id="chatterDownload" reRender="FeedFilePanel"   /><br/>


  <apex:outputPanel id="FeedFilePanel" rendered={!feedFile}>
                If you want to download the files that are attached to the Feeds then click below button<br/>
                
     
             <center>   <apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" /></center>
            </apex:outputPanel>

Apex class:
public PageReference Download(){  
     
       feedFile=true;
        
        if(!feedItemList.isEmpty()){
            String fileName = 'Feeds' +  DateTime.Now() + '.pdf';
            Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=' + fileName);
            PageReference chatterdownload= Page.ChatterDownload;
            chatterdownload.setRedirect(false);
            return chatterdownload; 
        } 
        else{
            
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Feeds, Please select an another Date to Download'));
            return null;
             
        }     
       
   }

Please someone telI me how to use Rerender attribute in that command button?

Thanks in advance
Karthick


 
sandeep sankhlasandeep sankhla
Hi SS,

Please use like below

<center>   <apex:commandButton rerender = "Dummy" id="uploadZipButton" value="Download Files"action="{!uploadZip}" /></center>

             </apex:outputPanel>

I have povided the id in rerender..you can provide the block id which you want to rerender.......provide the pageblock id 

Thanks
SS KarthickSS Karthick
@Sandeep,
      I want to use rerender attribute in first command button?
for that how can i implement
sandeep sankhlasandeep sankhla
Hi

For first one you ahve already implemented..if your download method will return null then it will rerender else it will redirect to the mentioned page as you mentioned in page

PageReference chatterdownload= Page.ChatterDownload;

         chatterdownload.setRedirect(false);
            return chatterdownload;

What issue you are facing ? It will redirect if it will execute above code

Thanks
SS KarthickSS Karthick

Hi Sandeep,
      For first one it doesnt rerender the output panel.
My use case is when i click the download button then it will download the page(second page ChatterDownload). but I don't want to redirect that page(for that am giving setRedirect false.).After that output panel must be rerender?

for that how can I implement
sandeep sankhlasandeep sankhla
So currently it is redirecting or not ? if not then it should rerender the block which you have provided...
Arunkumar RArunkumar R
Hi Karthik,

    You cannot use rendered and rerender attribute at the same time, To avoid this problem you need to create another outputpanel. Find the below code, it will be helpful to you..!
 
<apex:commandButton action="{!Download}" value="download" id="chatterDownload" reRender="FeedFilePanel" /><br/>


  <apex:outputPanel id="FeedFilePanel">
 <apex:outputPanel rendered={!feedFile}>
                If you want to download the files that are attached to the Feeds then click below button<br/>
                
     
             <center>   <apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" /></center>
            </apex:outputPanel>
</apex:outputPanel>
sandeep sankhlasandeep sankhla
Hi SS,

I agree with Arun, please use the code which he mentioned..it's a known behaviour or these 2 attributes..

Thansk
Mudasir WaniMudasir Wani
Hey SS,
The reason your code is not working is that if your panel is not rendered first time you can't reRender it.
That is why I have added another panel which will always load inside that is your panel 
Use the below code this will work
<apex:commandButton action="{!Download}" value="download" id="chatterDownload" reRender="FeedFilePanel"   /><br/>

 <apex:outputPanel id="FeedFilePanel">
  <apex:outputPanel id="FeedFilePanelTwo" rendered={!feedFile}>
                If you want to download the files that are attached to the Feeds then click below button<br/>
                
     
             <center>   <apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" /></center>
            </apex:outputPanel>
 </apex:outputPanel>


Donot forget to select best answer to make our efforts visible in the developer forum.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
SS KarthickSS Karthick
Yeah redirect is working but the rerender is not working.
I want to work both at the same time
 
SS KarthickSS Karthick
@Arun,Mudasir Wani:
      Thanks for your response.I have changed my code.
But again it doesnt reRender ..
the code is
public PageReference Download(){  
     
      
        
        if(!feedItemList.isEmpty()){
           feedFile=true;         

            String fileName = 'Feeds' +  DateTime.Now() + '.pdf';
            Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=' + fileName);
            PageReference chatterdownload= Page.ChatterDownload;
            chatterdownload.setRedirect(false);
            return chatterdownload; 
        } 
        else{
            
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Feeds, Please select an another Date to Download'));
            return null;
             
        }     
       
   }



VFP:

 <apex:outputPanel id="Parent">
            <apex:pageBlockSection title="Select Date to download chatter feeds" collapsible="false" >
                
                           
                
                <apex:commandButton action="{!Download}" value="download" id="chatterDownload"  ReRender="Parent" /><br/>
                
            </apex:pageBlockSection>   
           
            <apex:outputPanel id="FeedFilePanel"  rendered="{!feedFile}">
                If you want to download the files that are attached to the Feeds then click below button<br/>
                <apex:inputHidden id="zipContent" value="{!zipContent}" />
        
               
                <apex:inputHidden id="zipFileName" value="{!zipFileName}" />
     
             <center>   <apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" /></center>
            </apex:outputPanel> 
            </apex:outputPanel>

Thanks in advance
Karthick
Arunkumar RArunkumar R
Hi Karthick,

 The above changed code is working fine to me, can you share your full code to solve this problem.
SS KarthickSS Karthick
@Arunkumar:
         
VFP:


<apex:page controller="ChatterArchiver" sidebar="false" >



    <apex:form id="archiverForm">
        <apex:pageBlock title="Archiver For Chatter" id="DateBlock">
            <center><apex:pageMessages escape="false" ></apex:pageMessages></center>
            <apex:outputPanel id="parent">
            <apex:pageBlockSection title="Select Date to download chatter feeds" collapsible="false" >
                
                <apex:selectList value="{!selectedDate}" multiselect="false" size="1" label="Select Date" id="SelectList" >
                    <apex:selectOptions value="{!dateOptions}"/>
                </apex:selectList>             
                
                <apex:commandButton action="{!Download}" value="download" id="chatterDownload"  Rerender="parent"  /><br/>
                
            </apex:pageBlockSection>   
           
            <apex:outputPanel id="FeedFilePanel" rendered="{!renderPanel}">
                If you want to download the files that are attached to the Feeds then click below button<br/>
                <apex:inputHidden id="zipContent" value="{!zipContent}" />
        
               
                <apex:inputHidden id="zipFileName" value="{!zipFileName}" />
     
             <center>   <apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" /></center>
            </apex:outputPanel> 
            </apex:outputPanel>
            
        </apex:pageBlock>        
    </apex:form>  
</apex:page>



Controller:

public class ChatterArchiver {
    public String selectDate{get;set;}
    public List<FeedItem> feedItemList{get;set;}
    public String query{get;set;}
    
    public boolean renderPanel{get;set;}
    
    public String zipFileName {get; set;}
    public String zipContent {get; set;}
    
    public Static string selectedDate {get;set;}
    
    public ChatterArchiver(){
        
        feedItemList=new List<FeedItem>();
        zipFileName='FeedFiles' +  DateTime.Now() + '.zip'; 
        renderPanel=false;
        
        //feedItemList=[SELECT Id,Type,CreatedDate,CreatedById,CreatedBy.FirstName,CreatedBy.LastName,ParentId,Parent.Name,Body,Title,LinkUrl,ContentData,ContentFileName,LikeCount,CommentCount,(SELECT Id,CommentBody,CreatedDate,CreatedBy.FirstName,CreatedBy.LastName FROM FeedComments),(SELECT CreatedBy.FirstName,CreatedBy.LastName FROM FeedLikes) FROM FeedItem ];

        //feedItemList=[SELECT Id,Type,CreatedDate,CreatedById,CreatedBy.FirstName,CreatedBy.LastName,ParentId,Parent.Name,Body,Title,LinkUrl,ContentData,ContentFileName,LikeCount,CommentCount,(SELECT Id,CommentBody,CreatedDate,CreatedBy.FirstName,CreatedBy.LastName FROM FeedComments ),(SELECT CreatedBy.FirstName,CreatedBy.LastName FROM FeedLikes) FROM FeedItem where CreatedDate = Today limit 1 ];
    }
    
    public List<SelectOption> getDateOptions() {
        List<SelectOption> dateoption = new List<SelectOption>();
       
        dateoption.add(new SelectOption('YESTERDAY','YESTERDAY'));
        dateoption.add(new SelectOption('TODAY','TODAY'));
        dateoption.add(new SelectOption('LAST_WEEK','LAST_WEEK'));
        dateoption.add(new SelectOption('THIS_WEEK','THIS_WEEK'));
        dateoption.add(new SelectOption('LAST_MONTH','LAST_MONTH'));
        dateoption.add(new SelectOption('THIS_MONTH','THIS_MONTH'));
        dateoption.add(new SelectOption('LAST_90_DAYS','LAST_90_DAYS'));
        dateoption.add(new SelectOption('THIS_QUARTER','THIS_QUARTER'));
        dateoption.add(new SelectOption('LAST_QUARTER','LAST_QUARTER'));
        dateoption.add(new SelectOption('THIS_YEAR','THIS_YEAR'));
        dateoption.add(new SelectOption('LAST_YEAR','LAST_YEAR'));
 
        return dateoption;
    }
    
    
    public PageReference Download(){  
       // selectDate1=selectDate;
        
        renderPanel=true;
        query= 'select Id,RelatedRecordId,Type,Body,CreatedDate,CreatedBy.FirstName,CreatedBy.LastName,Title,LinkUrl,LikeCount,CommentCount,(SELECT Id,RelatedRecordId,CommentBody,CreatedDate,CreatedBy.FirstName,CreatedBy.LastName FROM FeedComments ORDER BY CreatedDate),(SELECT CreatedBy.FirstName,CreatedBy.LastName FROM FeedLikes) FROM FeedItem where CreatedDate =' +selectedDate +'';

        feedItemList=(Database.query(query));

       
        
        if(!feedItemList.isEmpty()){
            String fileName = 'Feeds' +  DateTime.Now() + '.pdf';
            Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=' + fileName);
            PageReference chatterdownload= Page.ChatterDownload;
            chatterdownload.setRedirect(false);
            return chatterdownload; 
        } 
        else{
            
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Feeds, Please select an another Date to Download'));
            return null;
             
        }     
       
   }
    public PageReference uploadZip() {
        if (String.isEmpty(zipFileName) ||
            String.isBlank(zipFileName)) {
            zipFileName = 'my_zip.zip';
        }
        else {
            zipFileName.replace('.', '');
            zipFileName += '.zip';
        }
         
        Document doc = new Document();
        doc.Name = zipFileName;
        doc.ContentType = 'application/zip';
        doc.FolderId = UserInfo.getUserId();
        doc.Body = EncodingUtil.base64Decode(zipContent);
         
        insert doc;
         
        this.zipFileName = null;
        this.zipContent = null;
 
        PageReference pageRef = new PageReference('/servlet/servlet.FileDownload?file=' + doc.Id);
        pageRef.setRedirect(false);
         
        return pageRef;
    }
   
   
}

after downloading the second page(chatterDownload) I wnat to rerender the output panel .But above code is not working ...
Mudasir WaniMudasir Wani
Hello,

Use this code and let me know if any issue
<apex:outputPanel id="Parent">
	<apex:pageBlockSection title="Select Date to download chatter feeds" collapsible="false" >
		<apex:commandButton action="{!Download}" value="download" id="chatterDownload"  ReRender="FeedFilePanel,FeedFilePanelOne" /><br/>
	</apex:pageBlockSection> 
	<apex:outputPanel id="FeedFilePanelOne">	
		<apex:outputPanel id="FeedFilePanel"  rendered="{!feedFile}">
			If you want to download the files that are attached to the Feeds then click below button<br/>
			<apex:inputHidden id="zipContent" value="{!zipContent}" />
			<apex:inputHidden id="zipFileName" value="{!zipFileName}" />
			<center>   
				<apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" />
			</center>
		</apex:outputPanel> 
	</apex:outputPanel> 
</apex:outputPanel>


Donot forget to select best answer to make our efforts visible in the developer forum.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
 
SS KarthickSS Karthick
@Arun: the problem is if i use rerender attribute then Download method is not working
SS KarthickSS Karthick
@Arun: again is not working
Mudasir WaniMudasir Wani
Hey SS Karthick,
Use the below page 
 
<apex:page controller="ChatterArchiver" sidebar="false" >



    <apex:form id="archiverForm">
        <apex:pageBlock title="Archiver For Chatter" id="DateBlock">
            <center>
				<apex:pageMessages escape="false" ></apex:pageMessages>
			</center>
            <apex:outputPanel id="parent">
				<apex:pageBlockSection title="Select Date to download chatter feeds" collapsible="false" >
					
					<apex:selectList value="{!selectedDate}" multiselect="false" size="1" label="Select Date" id="SelectList" >
						<apex:selectOptions value="{!dateOptions}"/>
					</apex:selectList>             
					
					<apex:commandButton action="{!Download}" value="download" id="chatterDownload"  reRender="FeedFilePanelOne,FeedFilePanel"  /><br/>
					
				</apex:pageBlockSection>   
			    <apex:outputPanel id="FeedFilePanelOne">
					<apex:outputPanel id="FeedFilePanel" rendered="{!renderPanel}">
						If you want to download the files that are attached to the Feeds then click below button<br/>
						<apex:inputHidden id="zipContent" value="{!zipContent}" />
				
					   
						<apex:inputHidden id="zipFileName" value="{!zipFileName}" />
			 
					 <center>   <apex:commandButton id="uploadZipButton" value="Download Files" action="{!uploadZip}" /></center>
					</apex:outputPanel> 
				</apex:outputPanel> 
            </apex:outputPanel>
            
        </apex:pageBlock>        
    </apex:form>  
</apex:page>


Donot forget to select best answer to make our efforts visible in the developer forum.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
 
SS KarthickSS Karthick
Hi Mudasir Wani,
        It's not working..The action method Download use PageReferecne class.
public PageReference Download(){  
       // selectDate1=selectDate;
        
        renderPanel=true;
        query= 'select Id,RelatedRecordId,Type,Body,CreatedDate,CreatedBy.FirstName,CreatedBy.LastName,Title,LinkUrl,LikeCount,CommentCount,(SELECT Id,RelatedRecordId,CommentBody,CreatedDate,CreatedBy.FirstName,CreatedBy.LastName FROM FeedComments ORDER BY CreatedDate),(SELECT CreatedBy.FirstName,CreatedBy.LastName FROM FeedLikes) FROM FeedItem where CreatedDate =' +selectedDate +'';

        feedItemList=(Database.query(query));

       
        
        if(!feedItemList.isEmpty()){
            String fileName = 'Feeds' +  DateTime.Now() + '.pdf';
            Apexpages.currentPage().getHeaders().put('content-disposition', 'attachment; filename=' + fileName);
            PageReference chatterdownload= Page.ChatterDownload;
            chatterdownload.setRedirect(false);
            return chatterdownload; 
        } 
        else{
            
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Feeds, Please select an another Date to Download'));
            return null;
             
        }     
       
   }

 
Mudasir WaniMudasir Wani
Brother let us make it simple 
Just comment all the code in your Download function.
Just make the field value as true and check if It works.



 
SS KarthickSS Karthick
@Mudasir,
            I know how to use rerender attribute...
But I want to know how we can use rerender attribute when the action method using page reference.?
 
Mudasir WaniMudasir Wani
Hi SS Karthick,

I have done a simple example for your understanding.
Please read the comments in the page and let me know if you have any question.

Page ---
<apex:page controller="reRenderTest">
    <apex:form>
        <apex:outputPanel id="one">
            The panel number one
        </apex:outputPanel>
        <br/>
        <!--Panel two will never be rendered as it is not rendered first timel -->
        <apex:outputPanel id="two" rendered="{!showBlockOne}">
            The panel number Two
        </apex:outputPanel>
        <br/>
        <!--I order to make the content of panel three visible wrap it in another panel and reRender that panel  -->
        <!--I am wrapping the panel three with another panel -->
        <apex:outputPanel id="newPanel">
            <apex:outputPanel id="three" rendered="{!showBlockOne}">
                The panel number Three is Visible now
            </apex:outputPanel>
        </apex:outputPanel>
        <br/>
        <apex:commandButton action="{!showPanel}" value="Show Panels" reRender="one,two,three,newPanel"></apex:commandButton>
    </apex:form>
</apex:page>
Class--
 
public class reRenderTest {
    
    public boolean showBlockOne{get;set;}
    
    //Constructor
    public reRenderTest(){
        showBlockOne  = false;
    }
    public pageReference showPanel(){
        showBlockOne  = true;
        return null;
    }
}



Donot forget to select best answer to make our efforts visible in the developer forum.

Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help