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
Ankit Khurana24Ankit Khurana24 

how to perform save action in the custom object's vf page?

Chamil MadusankaChamil Madusanka

There are few ways to save records through custom page


1st method: use only standardController at the beginning of apex markup without using
custom controller or extensions
• inputField will be appeared as follow;

<apex:inputField value="{!Applicant__c.Name__c}"/>
• commandButton
<apex:commandButton id="submitApplicant" value="Submit"
action="{!save}"/>

 
2nd method: Use Standard controller with an extension
• inputField

<apex:inputField value="{!Applicant__c.Name__c}"/>

 • commandButton

<apex:commandButton id="submitApplicant" value="Submit"
action="{!saveApplication}"/>

 
• custom controller must have below methods

public status(ApexPages.StandardController stdController) {
this.applicant=(Applicant__c)stdController.getRecord();
}
public PageReference saveApplication() {
//IF you need more manupulation to your saving record, that code here
insert(applicant);

} }

 


3rd method: Use Standard controller with an extension and inputText
• InputText

<apex:inputtext value="{!name}"/>

 
• ComandButton (no chnage)

<apex:commandButton id="submitApplicant" value="Submit"
action="{!saveApplication}"/>

 
• Custom Controller must have following property

public String name {get; set;}

 
• Following code will appear in saveApplication method

public PageReference saveApplication() {
applicant = new Applicant__c(); applicant.Name__c=name;
insert applicant;
}

 

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

asish1989asish1989

HI 

     Try this

       <apex:page controller="book" >

             <apex:form >

                  <apex:pageBlock title="Book Details">

                         <apex:pageBlockSection title="Books" columns="2" >

                              <apex:inputField value="{!book.Title__c}" label="Title"/>
                             <apex:inputField value="{!book.Author__c}" label="Author"/>
                             <apex:inputField value="{!book.Price__c}" label="Price"/>
                            <apex:inputField value="{!book.Publisher__c}" label="Publisher"/>     

                   </apex:pageBlockSection>

             

<apex:pageBlockButtons location="top">
<apex:commandButton value="save" action="{!save}"/>

</apex:pageBlockButtons>

</apex:pageBlock>

 </apex:form >

</apex:page>

 

Now controller is 

   

public class book {

           public  Book__c book;

           public book(){

              Book__c book = new Book__c();

           }

       public pageReference save(){

                 upsert/Insert book;

                  reyurn null;

       }

}

Did this post answers your issue If so please mark it solved

 

thanks

asish

 

Ankit Khurana24Ankit Khurana24

this is my vf page...... there is save button on each record,,,,so when record is edited and save is clicked records should be updated and page shoul be refreshed......

 

<apex:page controller="ContactGrid" id="thepage" >
<apex:form id="theform" >
<script>
function saveorg()
{
alert('alert in save org '+document.getElementById("{!$Component.thepage.theform.theblock.thetable}").value);
saverecord(document.getElementById("{!$Component.thepage.theform.theblock.thetable}").value);
}



</script>
<apex:pageBlock title="Contact Grid" id="theblock" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<apex:commandButton value="Create New Button"/>
<apex:pageBlockTable width="80%" columns="6" value="{!cppz}" var="o" id="thetable">
<apex:column headerValue="Action" >
<apex:commandButton value="Save" id="savebutton"/>
</apex:column>
<apex:column headerValue="Last Name" >
<apex:inputField value="{!o.LastName}"/>

</apex:column>
<apex:column headerValue="First Name" >
<apex:inputField value="{!o.FirstName}"/>
</apex:column>
<apex:column headerValue="Email" >
<apex:inputField value="{!o.Email}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!o.Phone}"/>
</apex:column>
<apex:column headerValue="Account Name" >
<apex:inputField value="{!o.AccountId }"/>
</apex:column>
</apex:pageBlockTable>


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

 

 

this my class....

public class ContactGrid
{
public ContactGrid__c ContactGrid

public List<Contact> cppz{get;set;}
public string retid{get;set;}
public ContactGrid(ApexPages.StandardController controller)
{
ContactGrid__c ContactGrid= new ContactGrid__c();
}

public List<Contact> getCppz()
{
cppz=[Select c.Phone, c.LastName, c.FirstName, c.Email, c.AccountId From Contact c];
return cppz;

}
public pageReference save()
{
Insert ContactGrid;
return null;

}
}

asish1989asish1989

Hi

    Try this

      public Id yourpageid{get;set;}

        public pageReference save()
         {
            upsert ContactGrid;

            yourpageid = ApexPages.currentPage().getParameters().get('optId');

             PageReference samepage=new PageReference('/apex/yourpagename?id='+yourpageid);
             samepage.setRedirect(true);

             return samepage;

         }

 

Did this post answers your question If so please mark it solved.

 

Thanks

asish

 

 

 

Chamil MadusankaChamil Madusanka

Do following highlighted changes

 

<apex:page controller="ContactGrid" id="thepage" >
<apex:form id="theform" >
<script>
function saveorg()
{
alert('alert in save org '+document.getElementById("{!$Component.thepage.theform.theblock.thetable}").value);
saverecord(document.getElementById("{!$Component.thepage.theform.theblock.thetable}").value);
}



</script>
<apex:pageBlock title="Contact Grid" id="theblock" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<apex:commandButton value="Create New Button"/>
<apex:pageBlockTable width="80%" columns="6" value="{!cppz}" var="o" id="thetable">
<apex:column headerValue="Action" >
<apex:commandButton value="Save" id="savebutton" action="{!saveContact}"/>
</apex:column>
<apex:column headerValue="Last Name" >
<apex:inputField value="{!o.LastName}"/>

</apex:column>
<apex:column headerValue="First Name" >
<apex:inputField value="{!o.FirstName}"/>
</apex:column>
<apex:column headerValue="Email" >
<apex:inputField value="{!o.Email}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!o.Phone}"/>
</apex:column>
<apex:column headerValue="Account Name" >
<apex:inputField value="{!o.AccountId }"/>
</apex:column>
</apex:pageBlockTable>


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

 

public class ContactGrid
{
public ContactGrid__c ContactGrid

public List<Contact> cppz{get;set;}
public string retid{get;set;}
public ContactGrid(ApexPages.StandardController controller)
{
ContactGrid__c ContactGrid= new ContactGrid__c();
}

public List<Contact> getCppz()
{
cppz=[Select c.Phone, c.LastName, c.FirstName, c.Email, c.AccountId From Contact c];
return cppz;

}
public pageReference saveContact()
{
Insert cppz;
return null;

}
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Ankit Khurana24Ankit Khurana24

 HI  Chamil,

IT DOESN'T WORK....ACTUALLY input fields are editable ..so when do i change them and click save ..changes should be saved..

Chamil MadusankaChamil Madusanka

After the save, you need to render outputfields.

 

<apex:column headerValue="First Name" >
<apex:inputField value="{!o.FirstName}" rendered="{!Displayout == false}"/>
<apex:outputField value="{!o.FirstName}" rendered="{!Displayout}"/>
</apex:column>

 

 

 

public class ContactGrid
{
public ContactGrid__c ContactGrid
public Boolean Displayout{get;set;}

public List<Contact> cppz{get;set;}
public string retid{get;set;}
public ContactGrid(ApexPages.StandardController controller)
{
ContactGrid__c ContactGrid= new ContactGrid__c();
Displayout = false;
}

public List<Contact> getCppz()
{
cppz=[Select c.Phone, c.LastName, c.FirstName, c.Email, c.AccountId From Contact c];
return cppz;

}
public pageReference save()
{
Insert ContactGrid;
Displayout = true;
return null;

}
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Ankit Khurana24Ankit Khurana24

donot works:On being click  on save it returns to the same page without saving the changes,,

Ankit Khurana24Ankit Khurana24

public class ContactGrid

{
public Boolean Displayout{get;set;}
public List<Contact> con{get;set;}
public List<Contact> cppz;
public Id retid{get;set;}

public ContactGrid(){}
public List<Contact> getCppz()
{
cppz=[Select c.Phone, c.LastName, c.FirstName, c.Email, c.AccountId From Contact c];
return cppz;

}

public pageReference saveContact()
{
upsert cppz;
Displayout = true;
return null;

}

 

}

 

 

vf page

<apex:page controller="ContactGrid" id="thepage" >
<apex:form id="theform" >



<apex:pageBlock title="Contact Grid" id="theblock" >
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<apex:commandButton value="Create New Button"/>
<apex:pageBlockTable width="80%" columns="6" value="{!cppz}" var="o" id="thetable">
<apex:column headerValue="Action" >
<apex:commandButton value="Save" id="savebutton" action="{!saveContact}" rendered="{!Displayout == false}"/>
</apex:column>
<apex:column headerValue="Last Name" >
<apex:inputField value="{!o.LastName}" rendered="{!Displayout == false}"/>

</apex:column>
<apex:column headerValue="First Name" >
<apex:inputField value="{!o.FirstName}" rendered="{!Displayout == false}"/>
</apex:column>
<apex:column headerValue="Email" >
<apex:inputField value="{!o.Email}" rendered="{!Displayout == false}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:inputField value="{!o.Phone}" rendered="{!Displayout == false}"/>
</apex:column>
<apex:column headerValue="Account Name" >
<apex:inputField value="{!o.AccountId }" rendered="{!Displayout == false}"/>
</apex:column>
</apex:pageBlockTable>


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