You need to sign in to do that
Don't have an account?
Sudhir_Meru
Insert Record into custom object
Hi,
I am new to SalesForce and Am newbie learning Visual Force and Apex Codding. I created a new custom object with name customer_details_c and desinged a page using visual force.
In customer_details_c object it has First_Name_c and Last_Name_c as two custom fields.
Now My requirement is to create a class or controller to insert the data into customer_details_c object. Please suggest me know. How to write the class or controler with an example.
Thanks
Sudhir
You can use the following code of Visualforce Page to insert the record without using Apex Class and method to save the record.
<apex:page standardController="customer_details_c" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >First Name</apex:outputLabel>
<apex:inputField id="FirstName" value="{!customer_details_c.First_Name_c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Last name</apex:outputLabel>
<apex:inputField id="LastName" value="{!customer_details_c.Last_Name_c}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Please try it.
All Answers
Hi Sudhir,
Try below code :-
VF page : -
<apex:page standardController="customer_details_c" extensions="insertnewrecordController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >First Name</apex:outputLabel>
<apex:inputField id="FirstName" value="{!cds.First_Name_c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Last name</apex:outputLabel>
<apex:inputField id="LastName" value="{!cds.Last_Name_c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class :-
public class insertnewrecordController{
public customer_details_c cds{get; set;}
public customer_details_c setcds(customer_details_c op){
this.cds=op;
return cds;
}
public insertnewrecordController(ApexPages.StandardController controller) {
cds=new customer_details_c();
}
public pagereference Save(){
customer_details_c cd = new customer_details_c();
cd.First_Name_c=cds.First_Name_c;
cd.Last_Name_c=cds.Last_Name_c;
insert cd;
Pagereference pg = new Pagereference('/' + cd.id);
pg.setredirect(true);
return pg;
}
}
Hi Vinit,
Thank you so much for your quick responce will implement and give you feed back :)
Thanks
Sudhir
Hi Vinit,
Tried you method its working perfer, Would like to know is there any alternative way again to insert in the same way, In a simple method. Please suggest.
Thanks
Sudhir
You can use the following code of Visualforce Page to insert the record without using Apex Class and method to save the record.
<apex:page standardController="customer_details_c" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >First Name</apex:outputLabel>
<apex:inputField id="FirstName" value="{!customer_details_c.First_Name_c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Last name</apex:outputLabel>
<apex:inputField id="LastName" value="{!customer_details_c.Last_Name_c}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Please try it.
When you use standard Save Function i.e. {!Save}, it will be navigated to the new record detail page.
if you want to the user to enter another record, you have to use PageReference method via Extensions or Custom Controller.
Tried your code. Record is inserting perfectly, but in that record i can't able to view the values which i have entered in visualforce page.
Kindly,help me out of this.
I tried your solution Its working fine but the record gets saved with record id. Means if i type ABC in name it gets storeed with the record id instead of ABC any idea why this is hapening.