You need to sign in to do that
Don't have an account?
Nasir
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
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:
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:
Note that this is basically the same code that is used by the the standard UI when you save a record.
Thanks a lot.This worked.
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....?