0

I am working on VF Page, where I am inserting record and the moment it inserted it will appear in next pageblock and I used "RefreshPage" command but some how my page block "Details Of Attendees" is not getting refreshed.

Can anyone help me out in this issue if possible. My vf page code is below.

<apex:page Controller="StagingEventInsertAndShowData" lightningStylesheets="true">
<apex:form>
    <apex:pageBlock title="New Staging Event Attendee">
        <apex:pageBlockSection id="reRenderId">
            <apex:inputText label="Barcode" value="{!Barcode}"/>
            <apex:commandButton value="Insert" action="{!save}" reRender="reRenderId" oncomplete="refreshPage();"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
    <apex:pageBlock title="Details of Attendees">
        <apex:pageBlockTable value="{!accList}" var="a">
            <apex:column headerValue="Barcode" value="{!a.Barcode__c}"/>
            <apex:column headerValue="Attendant" value="{!a.Attendant__c}"/>
            <apex:column headerValue="Campaign" value="{!a.Campaign_ID__c}"/>
            <apex:column headerValue="Email" value="{!a.Email__c}"/>
            <apex:column headerValue="Date Attended" value="{!a.Date_Attended__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>

Controller:

public class StagingEventInsertAndShowData {
    public String Barcode{get;set;}
    public List<Staging_Event_Attendee__c> accList {get;set;}

    public StagingEventInsertAndShowData(){
        accList=[SELECT Name,Barcode__c,Attendant__c,Campaign_ID__c,Email__c,Date_Attended__c FROM Staging_Event_Attendee__c Barcode__c ORDER BY Barcode__c ASC LIMIT 100];
    }

    public PageReference save() {
        Staging_Event_Attendee__c ac = new Staging_Event_Attendee__c();
        ac.Barcode__c = Barcode;
        insert ac;
        this.Barcode = null;
        return null;
    }
}

3 Answers 3

0

Try this:

Page block:

<apex:pageBlock id="reRenderId" title="Details of Attendees">

oncomplete="refreshPage();" is not needed on commandbutton

0

You need to rerender the page block where the account details are populating, update the second-page block like below:

<apex:pageBlock title="Details of Attendees" id="reRenderId">

Whenever the record is saved, it will look for the reRenderId to refresh that particular block.

oncomplete="refreshPage();" is javascript function which is not required in your case.

Checkout this example : reRender example

0

There are couple of things you need to change in your code to make this work.

  • You will have to rerender the pageBlock (in addition to the pageBlockSection with inputText) on click of Insert button so as to refresh that pageBlockTable to show you the updated records including the new one which got inserted. Also, there is no need to call refreshPage(); from commandButton.
  • You are getting the records in constructor, hence it will always display the same records even after you rerender the pageBlockTable i.e. it won't include the record that was inserted, this is because, your constructor would not get called on click of Insert. You can go through Order of Execution in a Visualforce Page to get more information on this topic. So to make this work, you will either have to create a getter for accList or get all the records again after insertion.

So your visualforce page would be

<apex:page Controller="StagingEventInsertAndShowData" lightningStylesheets="true">
    <apex:form>
        <apex:pageBlock title="New Staging Event Attendee">
            <apex:pageBlockSection id="reRenderId">
                <apex:inputText label="Barcode" value="{!Barcode}"/>
                <apex:commandButton value="Insert" action="{!save}" reRender="reRenderId,pageBlockRecords"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock title="Details of Attendees" id="pageBlockRecords">
            <apex:pageBlockTable value="{!accList}" var="a">
                <apex:column headerValue="Barcode" value="{!a.Barcode__c}"/>
                <apex:column headerValue="Attendant" value="{!a.Attendant__c}"/>
                <apex:column headerValue="Campaign" value="{!a.Campaign_ID__c}"/>
                <apex:column headerValue="Email" value="{!a.Email__c}"/>
                <apex:column headerValue="Date Attended" value="{!a.Date_Attended__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

And controller should be

public class StagingEventInsertAndShowData {
    public String Barcode{get;set;}
    public List<Staging_Event_Attendee__c> accList {get;set;}

    public StagingEventInsertAndShowData(){
        getStagingRecords();
    }

    public void getStagingRecords(){
        accList=[SELECT Name,Barcode__c,Attendant__c,Campaign_ID__c,Email__c,Date_Attended__c FROM Staging_Event_Attendee__c Barcode__c ORDER BY Barcode__c ASC LIMIT 100];
    }

    public PageReference save() {
        Staging_Event_Attendee__c ac = new Staging_Event_Attendee__c();
        ac.Barcode__c = Barcode;
        insert ac;
        this.Barcode = null;
        getStagingRecords();
        return null;
    }
}
0

You must log in to answer this question.