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
Rahul Gupta 137Rahul Gupta 137 

Vf Page having two sections

In a visual force page, i want to create two section one for creating records for the object . As soon as I hit submit button In the second section of page record that got should get the display in a table. It should have at least 5 last record

Cloud AtlasCloud Atlas

I had built something like that like 2 -3 years back.. 

Check this out..
Please note, that this is still just test code.
The final version was very different from this.
But I am sure, this will give you a starting point.

Change the object and field names to what you have in your environment.

Visualforce Page

<apex:page standardcontroller="Rules__c" extensions="addRules"  sidebar="false" >
    <apex:form >
        <apex:pageblock id="theRuleRec" >
            <apex:pageblocksection columns="1" id="thePagSec">
                <apex:outputLabel value="Selection">
                    <apex:inputField value="{!rulRec.Selection__c}"/>
                </apex:outputLabel>
            
                <apex:pageBlockSectionitem >
                    <apex:outputLabel value="Values">
                        <apex:inputField value="{!rulRec.Values__c}"/>
                    </apex:outputLabel>
                </apex:pageBlockSectionitem>
            
                <apex:outputlabel value="Destination">
                    <apex:inputField value="{!rulRec.Destination_Name__c}"/>
                </apex:outputLabel>
            </apex:pageBlockSection> 

            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"  />
                <apex:commandButton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageblock>
            <center>
                <apex:outputText value="{!strMsg}" id="msg" style="color:#FF0000"/>
            </center>
         <apex:pageblock id="theRuleLst" >
            <apex:pageblockTable value="{!RuleRecords}" var="rule"  rendered="{!if(editRec == true,false,true)}">
                <apex:column >
                    <apex:inputCheckbox value="{!rule.Old_Rule__c}" />
                </apex:column>
                <apex:column value="{!rule.Id}" />
                <apex:column value="{!rule.Selection__c}" />
                <apex:column value="{!rule.Values__c}" />
                <apex:column value="{!rule.Destination_Name__c}" />
                <!--
                <apex:column >
                    <apex:commandButton value="Edit" action="{!editRulLst}"/>
                </apex:column>
               -->
            </apex:pageblockTable> 
            
         
            <apex:pageblockTable value="{!RuleRecords}" var="rule" rendered="{!if(editRec == true,true,false)}" id="theRuleLstEdit">
                
                <apex:column >
                    <apex:inputCheckbox value="{!rule.Old_Rule__c}" />
                </apex:column>
                <apex:column value="{!rule.Id}" />
                   
                <apex:column headerValue="Selection" >
                    <apex:inputfield value="{!rule.Selection__c}"/>
                </apex:column>
                <apex:column headerValue="Values" >
                    <apex:inputfield value="{!rule.Values__c}"/>
                </apex:column>
                <apex:column headerValue="Destination Name" >
                    <apex:inputField value="{!rule.Destination_Name__c}"/>
                </apex:column>
            </apex:pageblockTable> 
                
            <apex:pageBlockButtons >
                
                <apex:commandButton value="Update" action="{!updateRulLst}" rerender="theRuleLst,thePagSec" rendered="{!if(editRec == true,true,false)}" disabled="{!if(RuleRecords.size = 0,true,false)}"/> 
                <apex:commandButton value="Edit" action="{!editRulLst}" rendered="{!if(editRec == false,true,false)}" id="theRuleRec" disabled="{!if(RuleRecords.size = 0,true,false)}"/> 
                <apex:commandButton value="Delete" action="{!deleteRulLst}" rerender="theRuleLst,thePagSec,msg" rendered="{!if(editRec == true,true,false)}" disabled="{!if(RuleRecords.size = 0,true,false)}"/>
                <apex:commandButton value="Cancel" action="{!cancelLst}" rerender="theRuleLst,thePagSec" rendered="{!if(editRec == true,true,false)}"/>
            </apex:pageBlockButtons>   
        </apex:pageblock> 
        

    </apex:form>  
</apex:page>

Apex Controller
 
Public class addRules{ 

    Public Rules__c rulRec{get; set;}
    Public List<Rules__c> ruleLst{get; set;}
    Public Boolean editRec{get; set;}
    Public String strMsg{get; set;}
    Map<Id,Rules__c> updateRulsBackUp = new Map<Id,Rules__c>();
    
    public addRules(ApexPages.StandardController Controller) { 
        rulRec = new Rules__c();
        editRec = false;
        strMsg = '';
    } 
    public PageReference Save() { 
        PageReference pagRef = new PageReference('/apex/Test1');
        insert rulRec; 
        rulRec = new Rules__c();
        pagRef.setRedirect(true);
     return pagRef;
    } 
    
    public List<Rules__c> getRuleRecords(){
        updateRulsBackUp = new Map<Id,Rules__c>();
        ruleLst = new List<Rules__c>();
        if(editRec)
        {
            ruleLst = [select id,Name,Old_Rule__c,Selection__c,Values__c,Destination_Name__c from Rules__c where Old_Rule__c =: true];
            for(Rules__c rulIterate : ruleLst)
            {
                if(rulIterate.Old_Rule__c)
                {
                    rulIterate.Old_Rule__c = false;
                    updateRulsBackUp.put(rulIterate.Id,rulIterate);
                }
            }     
        }else{
           ruleLst = [select id,Name,Old_Rule__c,Selection__c,Values__c,Destination_Name__c from Rules__c];
        }    
          
        
        return ruleLst;
    }
    
    public void deleteRulLst(){
        strMsg = '';
        List<Rules__c> deleteRuls = new List<Rules__c>();
        for(Rules__c rulIterate : ruleLst)
        {
            if(rulIterate.Old_Rule__c)
            {
                deleteRuls.add(rulIterate);
            }    
        }
        if(!deleteRuls.isEmpty())
        {
            delete deleteRuls;
        }else{
            strMsg = 'Please select atleast one record.';
        } 
    }
    public void editRulLst(){
        strMsg = '';
        List<Rules__c> updateRuls = new List<Rules__c>();
        for(Rules__c rulIterate : ruleLst)
        {
            if(rulIterate.Old_Rule__c)
            {
                rulIterate.updateTrg__c = true;
                updateRuls.add(rulIterate);
            } 
              
        }
        if(!updateRuls.isEmpty())
        {
            update updateRuls;
            editRec = true;
        }else{
            strMsg = 'Please select atleast one record.';
        } 
        
        
    }
    public void updateRulLst(){
        strMsg = '';
        recordsUpdate(true);
        editRec = false;
    }
    public static Boolean isFromCancelMethod = false;
    public void cancelLst(){
        isFromCancelMethod = true;
        strMsg = '';
        recordsUpdate(false);
        editRec = false;
        
    }
    
    public void recordsUpdate(Boolean trgVal){
        strMsg = '';
        List<Rules__c> updateRuls = new List<Rules__c>();
         system.debug('@#########ruleLst'+ruleLst.size());
          system.debug('@#########ruleLst'+ruleLst);
        
        if(!ruleLst.isEmpty())
        {
            for(Rules__c rulIterate : ruleLst)
            {
                Rules__c rulToUpdate = new Rules__c();
                if(updateRulsBackUp.containsKey(rulIterate.Id))
                {
                    updateRuls.add(updateRulsBackUp.get(rulIterate.Id));
                }    
                updateRuls.add(rulIterate.clone());
            }
            system.debug('@#########updateRuls'+updateRuls.size());
                system.debug('@#########updateRuls val'+updateRuls);
            if(!updateRuls.isEmpty())
            {
                for(Rules__c rulIterate : updateRuls)
                {
                    rulIterate.Old_Rule__c = false;
                    rulIterate.updateTrg__c = trgVal;
                }
               if( !isFromCancelMethod ) { 
               upsert updateRuls;
            } 
            
            
        }
        }
        
        
    }
    
}

Hope this helps.