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
Satya413Satya413 

Refresh VF Page after method executes

Hi,

I need to refresh the whole VF Page once my delete method executes. I have wriiten the below code. When I delete a particular record, the record is getting deleted but the page is not refreshing to show the updated records. Please help. 

VF Page:

<apex:page controller="viewdept_con" showHeader="false " id="mypage">
 <apex:form >
  <apex:pageBlock >
   <apex:pageblockTable value="{!deptlist}" var="item"> 
     <apex:column headerValue="Action">
     <apex:commandLink value="Detail" action="{!deptdetail}"/><b> | </b>
     <apex:commandLink value="Del" action="{!deptdelete}" reRender="mypage">
     <apex:param name="deptid" value="{!item.id}" assignTo="{!deptid}"/>
     </apex:commandlink>
     </apex:column>
     <apex:column value="{!item.name}"/>
   </apex:pageblockTable>
  </apex:pageBlock>
 </apex:form>
</apex:page>

Controller:

public class viewdept_con {

   public list<department__c> deptlist { get; set; }
   public id deptid {get; set;}
   public department__c dept {get; set;}
   
   public viewdept_con()
   {
   deptlist = [select id, name from department__c];
   }
   
   public pagereference deptdetail()
   {
     pagereference pg = new pagereference('/apex/DepartmentDetail?deptid=' + deptid);
     return pg;
   }
   
   public pagereference deptdelete()
   {
     dept = [select id, name from department__c where id = :deptid];
     delete dept;
     return null;
   }
}

Thank you,
Satya
Best Answer chosen by Satya413
Shailendra Singh ParmarShailendra Singh Parmar
Hi Satya,
Their are 2 ways to refresh deleted records.
#1. after deleting your records, you can again initilize deptlist by querying records same as you are doing in constructor. Actually when your methods has return null it will not call constructor again and will not initalize list.
#2.  Implement delete methods to return VF page reference and set is redirect true like below.
public pagereference deptdelete()
   {
     dept = [select id, name from department__c where id = :deptid];
     delete dept;
     PageReference ref = new your page reference (Page.yourPagename)
     // set parameter if any like ref.getParameters().add('id', 'dsf');
     ref.setRedirect(true);
    return ref;
   }

hope that will help

Thanks!

All Answers

ADITYA PRAKASH 5ADITYA PRAKASH 5
Just remove this " reRender="mypage" from " <apex:commandLink value="Del" action="{!deptdelete}" reRender="mypage">". The page will refresh then on click.
Satya413Satya413
Hi Aditya, 

Thanks for your reply. I have tried removing the rerender element but it still remains the same. The page seems to refresh, however it still shows the deleted record. 

Thank you,
Satya
Shailendra Singh ParmarShailendra Singh Parmar
Hi Satya,
Their are 2 ways to refresh deleted records.
#1. after deleting your records, you can again initilize deptlist by querying records same as you are doing in constructor. Actually when your methods has return null it will not call constructor again and will not initalize list.
#2.  Implement delete methods to return VF page reference and set is redirect true like below.
public pagereference deptdelete()
   {
     dept = [select id, name from department__c where id = :deptid];
     delete dept;
     PageReference ref = new your page reference (Page.yourPagename)
     // set parameter if any like ref.getParameters().add('id', 'dsf');
     ref.setRedirect(true);
    return ref;
   }

hope that will help

Thanks!
This was selected as the best answer
Satya413Satya413
Hi Shailendra Singh,

Thanks for your response. It worked. Below is my code.

public pagereference deptdelete()
   {
     dept = [select id, name from department__c where id = :deptid];
     delete dept;
     pagereference pg = new pagereference('/apex/ViewDepartment');
     pg.setredirect(true);
     return pg;
   }

Thank you,
Satya
vinayk kumarvinayk kumar
thanks @Satya413...):
Shailendra Singh ParmarShailendra Singh Parmar
Remove rerender property from command button and page will refresh