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
Tobias HaggeTobias Hagge 

Save button that only saves the pageblock

Hello.

Even when having different pageblocks and the save button only appears in one block it automatically saves all pageblock values.
<apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
Is it possible to modify this code, so it only saves the relevant pageblock in which the button is?
Best Answer chosen by Tobias Hagge
Ravi Dutt SharmaRavi Dutt Sharma
VF Page :
<apex:page extensions="ClassExt" standardController="Class__c">
<apex:form >
<apex:pageBlock >

<apex:pageblockSection >
    <apex:outputText >Room Number</apex:outputText>
    <apex:inputText value="{!roomNumber}"/>
</apex:pageblockSection>
 
<apex:pageblockSection >
    <apex:outputText >Floor Number</apex:outputText>
    <apex:inputText value="{!floorNumber}"/>
    <apex:commandButton action="{!customSaveMethod}" value="Save"/>
</apex:pageblockSection>

</apex:pageBlock>
</apex:form>
</apex:page>
Extension :
public class ClassExt {

    public Integer roomNumber{get;set;}
    
    public Integer floorNumber{get;set;}
    
    public ClassExt(ApexPages.StandardController controller) {

    }

    public void customSaveMethod(){
        Class__c obj=new Class__c();
        obj.Floor_Number__c=this.floorNumber;
        insert obj;
    }
}


 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
You can create a custom Save button which will Save only those fields which are present in your pageBlock
Tobias HaggeTobias Hagge
Do you know if there is a detailed instruction in how to do that?
Ravi Dutt SharmaRavi Dutt Sharma
VF Page :
<apex:page extensions="ClassExt" standardController="Class__c">
<apex:form >
<apex:pageBlock >

<apex:pageblockSection >
    <apex:outputText >Room Number</apex:outputText>
    <apex:inputText value="{!roomNumber}"/>
</apex:pageblockSection>
 
<apex:pageblockSection >
    <apex:outputText >Floor Number</apex:outputText>
    <apex:inputText value="{!floorNumber}"/>
    <apex:commandButton action="{!customSaveMethod}" value="Save"/>
</apex:pageblockSection>

</apex:pageBlock>
</apex:form>
</apex:page>
Extension :
public class ClassExt {

    public Integer roomNumber{get;set;}
    
    public Integer floorNumber{get;set;}
    
    public ClassExt(ApexPages.StandardController controller) {

    }

    public void customSaveMethod(){
        Class__c obj=new Class__c();
        obj.Floor_Number__c=this.floorNumber;
        insert obj;
    }
}


 
This was selected as the best answer