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
Sudhir_MeruSudhir_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

Best Answer chosen by Admin (Salesforce Developers) 
KaminiKamini

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

Vinit_KumarVinit_Kumar

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;


}

 

}

Sudhir_MeruSudhir_Meru

Hi Vinit, 

 

  Thank you so much for your quick responce will implement and give you feed back :)

 

Thanks

Sudhir

Sudhir_MeruSudhir_Meru

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

KaminiKamini

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.

This was selected as the best answer
ShahTheTrainerShahTheTrainer

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.

Div403Div403
Hi Vinit_Kumar,

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.
Shiva Kumar 82Shiva Kumar 82
How can we implement the same via SOAP or REST API ?
Tanmoy DashTanmoy Dash
Hi Vinit 
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. 
Kundan KamalKundan Kamal
Hello how can i insert data from android ap side.with the help of webservice.I have created custom object.
Obaid KhanObaid Khan
Is there nay way not to write fields mentioned in object and rather call that  in a page in class