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
NasirNasir 

how to write a SAVE method just to save the record by using VF and APEX

 

public class Store {
public Store__c str;

    public store(ApexPages.StandardController controller) {
this.str = (Store__c)Controller.getRecord();

 }
    public Store__c getStr(){
    return str;
    }
    
    Public PageReference save(){
     
Store__c str =[Select id,Name__c,Address__c,Phone__c,Email__c from Store__c ];
    upsert str;
    return null;
    }

}

 

Hi 

 

 

I have created an object called store and there are some fields in that object which is defind in the VF page as under.I just want to write a controller which uses the save action and can save the record directly from controller.

 

As i am new in Apex.Please help me in writing this save method.

<apex:page standardController="Store__c" extensions="Store">
<apex:form id="nasir">
<apex:pageMessages />
<apex:pageBlock title="ApexCloudStore" mode="edit">
  <apex:pageBlockButtons >
     <apex:commandButton action="{!save}" value="save" >
     <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:commandButton>
  </apex:pageBlockButtons>
 <!--This is the first section-->
<apex:pageBlockSection Title="Store Information" columns="2">

<apex:pageBlockSectionItem >
<apex:outputText value="Name"/>
<apex:inputField value="{!Store__c.Name__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Address"/>
<apex:inputField value="{!Store__c.Address__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Email"/>
<apex:inputField value="{!Store__c.Email__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Phone"/>
<apex:inputField value="{!Store__c.Phone__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Website"/>
<apex:inputField value="{!Store__c.Website__c}"/>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>  

<apex:pageBlockSection title="Snacks" columns="2">
<apex:pageBlockSectionItem >
<apex:outputText value="Chips"/>
<apex:inputField value="{!Store__c.Chips__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Cold Drinks"/>
<apex:inputField value="{!Store__c.Cold_Drinks__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Biscuits"/>
<apex:inputField value="{!Store__c.Biscuits__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Juice"/>
<apex:inputField value="{!Store__c.Juice__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputText value="Pop Corn"/>
<apex:inputField value="{!Store__c.Pop_Corn__c}"/>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>
  

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

 When i am saving this from UI.My code is not getting saved and some times it give "id not defind and some times attempt to dereferance a null object,

 

Please help me in resolving the errors

 

Thanks

 

Nasir

 

sfdcfoxsfdcfox

The extension class is not needed in your example. The ApexPages.StandardController function automatically provides a built-in "save()" method that you can use directly from Visualforce. Use extensions only for custom logic on a page, and use apex:pageBlockSectionItem only for custom input types, such as a checkbox that is not a field on the record you are editing.

 

Thus, your page would be as follows:

 

 

<apex:page standardController="Store__c">
<apex:form id="nasir">
<apex:pageMessages />
<apex:pageBlock title="ApexCloudStore" mode="edit">
  <apex:pageBlockButtons >
     <apex:commandButton action="{!save}" value="save" >
     <apex:commandButton action="{!cancel}" value="Cancel"/>
    </apex:commandButton>
  </apex:pageBlockButtons>
 <!--This is the first section-->
<apex:pageBlockSection Title="Store Information" columns="2">
  <apex:inputField value="{!Store__c.Name__c}"/>
 <apex:inputField value="{!Store__c.Address__c}"/>
  <apex:inputField value="{!Store__c.Email__c}"/>
  <apex:inputField value="{!Store__c.Phone__c}"/>
  <apex:inputField value="{!Store__c.Website__c}"/>
</apex:pageBlockSection>  

<apex:pageBlockSection title="Snacks" columns="2">
  <apex:inputField value="{!Store__c.Chips__c}"/>
  <apex:inputField value="{!Store__c.Cold_Drinks__c}"/>
  <apex:inputField value="{!Store__c.Biscuits__c}"/>
  <apex:inputField value="{!Store__c.Juice__c}"/>
  <apex:inputField value="{!Store__c.Pop_Corn__c}"/>
</apex:pageBlockSectionItem>

</apex:pageBlockSection>

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

If you did need an extension, you would want to name the method something other than "save", such as "saveRecord", and the function might appear as follows:

 

 

 

public ApexPages.PageReference saveRecord() {
  // I can do custom logic here before I save the record.
  ApexPages.StandardController controller = new ApexPages.StandardController(str);
  try {
    controller.save();
  }
  catch(Exception e) {
    return null;
  }
  return controller.view();
}

Note that this is basically the same code that is used by the the standard UI when you save a record.

 

NasirNasir


Thanks a lot.This worked.

Thomas Reinman 18Thomas Reinman 18
I am facing a similar challenge where I need to navigate to a new Visualforce page after saving the new record. Hence, I added a simple extension to try to resolve these two actions. 

User-added image

The page is redirecting to the other Visualforce page, but the record is not being saved. 
I've noticed that the .save() method doesn't really factor in Validation rules the way the standard {!save} action DOES. 

For this requirement, I've also tried using an INSERT DML statement instead of the .save() method, and I've even tried to use the standard {!save} method in conjunction with some Javascript redirection. Nothing seems to save the record. 

Another important detail -- the data I'm trying to save as a new record is coming from three separate Visualforce pages. I know the data is stored in the viewstate because the standard 'Save' button creates a new record. However when attempting to use a custom 'save' via DML, an error pops up listing the Required fields that are missing (which were previously filled out on the 1st page).

Any suggestions....?