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
Chanagan SakulteeraChanagan Sakulteera 

I want to insert data to database

Hi all,
I'm new for salesforce. I want to save data into database.
How to add record, answer me please.
this is code one in visualforce page.
<apex:pageBlock title="Other">
            <apex:commandButton value="addRow" action="{!addRow}"/>
            <br/><br/>
            <table class="table table-bordered" cellspacing="0" cellpadding="0" style="width:300px;">
                <tr>
                    <td style="background-color: #87CEEB;" align="center"><b>Other Name</b></td>
                    <td style="background-color: #87CEEB;" align="center"><b>Other Number</b></td>
                </tr>
                <apex:repeat value="{!row}" var="item" >
                    <tr>
                        <td align="center"><apex:inputText value="{!otherName}" id="otherName" html-placeholder="Other Name"/></td>
                        <td align="center"><apex:inputText value="{!otherNum}" id="otherNumber" html-placeholder="Other Number"/></td>
                    </tr>
                </apex:repeat>
            </table>
        </apex:pageBlock>

This is code one in controller.
//-- Add Row --\\
    public void addRow(){
        integer num = 1;
        String s = '';
        if(row == null){
            row = new List<String>();
            for(integer i = 0 ; i < num ; i++){
                s += num+'';
                row.add(s);
            }
            num++;
        }else{
            for(integer i = 0 ; i < num ; i++){
                s += num+'';
                row.add(s);
            }
            num++;
        }
    }
    //-- Add Row --\\

This is code two in visualforce page.
<apex:pageBlock title="Other">
            <apex:commandButton value="addRow" action="{!addRow}"/>
            <br/><br/>
            <table class="table table-bordered" cellspacing="0" cellpadding="0" style="width:300px;">
                <tr>
                    <td style="background-color: #87CEEB;" align="center"><b>Other Name</b></td>
                    <td style="background-color: #87CEEB;" align="center"><b>Other Number</b></td>
                </tr>
                <apex:repeat value="{!addOtherProduct}" var="item" >
                    <tr>
                        <td align="center"><apex:inputText value="{!item.Other_Name__c}" id="otherName" html-placeholder="Other Name"/></td>
                        <td align="center"><apex:inputText value="{!item.Other_Number__c}" id="otherNumber" html-placeholder="Other Number"/></td>
                    </tr>
                </apex:repeat>
            </table>
        </apex:pageBlock>

//-- Add Row --\\
    public void addRow(){
        addOtherProduct.add(new Product_Service__c(Other_Name__c = otherName, Other_Number__c = otherNum));
    }
//-- Add Row --\\

how to insert to database answer for me please.
Aditya MohanAditya Mohan

In the controller class which you have used for this VF page, create a property with the return type as sObject type like:
 
public objectName insertObject{get; set;}

Also create a function which will be called when the record is saved like insertObjectFunc(). 

Inside this function insert your object using INSERT operation: INSERT insertObject

In the Visualforce page in the action of the save button refer the above function:

<apex:commandButton value="Save" action="{!insertObjectFunc}"/>