+ Start a Discussion
Sainath VenkatSainath Venkat 

VF Page to show only inserted record

Hello Everyone,

I am working on one visualforce page where I have a text field and save button, on entering data to text field and clicking on save button, I am creating the record.

Now I want to show the newly inserted record on vf page in a table, I did created vf page and apex class but unable to show the created record on vf page.

Can anyone helps me out in this issue if possible.

Visualforce Page:
<apex:page Controller="StagingEventInsertAndShowData" lightningStylesheets="true">
    <apex:form >
        <apex:pageMessages id="pageMessages"/>
        <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,pageMessages"/>
            </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:column headerValue="Invalid Barcode" value="{!a.Invalid_Barcode__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Controller:
public class StagingEventInsertAndShowData {
    public String Barcode{get;set;}
    public List<Staging_Event_Attendee__c> accList {get;set;}

    public StagingEventInsertAndShowData(){
        getStagingRecords();
    }

    public void getStagingRecords(){
        Date todayDate = Date.today() ;
        accList=[SELECT Id,Name,Barcode__c,Attendant__c,Campaign_ID__c,Email__c,Date_Attended__c,Invalid_Barcode__c FROM Staging_Event_Attendee__c LIMIT 10];
    }

    public PageReference save() {
        if(string.isNotBlank(Barcode)){
            Staging_Event_Attendee__c ac = new Staging_Event_Attendee__c();
            ac.Barcode__c = Barcode;
            insert ac;
            this.Barcode = null;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM , 'Saved Successfully'));
            getStagingRecords();
        } else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Barcode was left blank. Please enter a barcode'));
        }
        return null;
    }
}


 
Kancharla ChakravarthyKancharla Chakravarthy
Hello @Sainath

I've modified the code. Please use the below code.


VF Page:
<apex:page Controller="StagingEventInsertAndShowData" lightningStylesheets="true">
    <apex:form id="form">
        <apex:pageMessages id="pageMessages"/>
        <apex:pageBlock title="New Staging Event Attendee">
            <apex:pageBlockSection id="reRenderId">
                <apex:inputText label="Barcode" value="{!Barcode}"/>
                <apex:commandButton value="Insert" action="{!save}" reRender="form"/>
            </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:column headerValue="Invalid Barcode" value="{!a.Invalid_Barcode__c}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Apex Class:
 
public class StagingEventInsertAndShowData {
    public String Barcode{get;set;}
    public List<Staging_Event_Attendee__c> accList {get;set;}

    public StagingEventInsertAndShowData(){
        getStagingRecords();
    }

    public void getStagingRecords(){
        Date todayDate = Date.today() ;
        accList=[SELECT Id,Name,Barcode__c,Attendant__c,Campaign_ID__c,Email__c,Date_Attended__c,Invalid_Barcode__c FROM Staging_Event_Attendee__c LIMIT 10];
    }

    public PageReference save() {
        if(string.isNotBlank(Barcode)){
            Staging_Event_Attendee__c ac = new Staging_Event_Attendee__c();
            ac.Barcode__c = Barcode;
            insert ac;
            this.Barcode = null;
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM , 'Saved Successfully'));
            getStagingRecords();
           
        } else {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Error, 'Barcode was left blank. Please enter a barcode'));
        }
        return null;
    }
}

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks,
Chakravarthy