Insert Record using JavaScript Remoting with Visualforce Page

Insert Record using JavaScript Remoting with Visualforce Page

Hey there!

We must have heard about javascript remoting. So today we are going to learn about
how to use the Javascript remoting to build Web apps and insert the record with the
Visualforce.The DML operation directly enables for the sObject from Javascript in remote objects.
So here I am going to tell you Insert the record with Visualforce page using Javascript remoting.

Here I am using the custom table and Remote object within Visualforce page:

Here is the code snippet:

<apex:page id="pageId">
        <apex:remoteObjectModel name="Account" fields="Id,Name,Industry,AccountNumber">
        var createAccount = function(){
                var Accname=document.getElementById("accname").value;
                var Accindustry=document.getElementById("accind").value;
                var Accnumber=document.getElementById("accnum").value;
                var acDetails = { Name: Accname, Industry: Accindustry,AccountNumber:Accnumber };
                var ct = new SObjectModel.Account();
                ct.create(acDetails , function(err) {
                    if(err) {
                    else {
                        console.log(ct.get('Id')); // Id is set when create completes
                        alert('account created successfully with id'+ct.get('Id'));
    <h1 style="font-size:20px;">
            Create Account via Remote Objects
            <td>Account Name:</td><td><input type="text" name="name" id="accname"/></td>
            <td>Industry:</td><td><input type="text" name="industry" id="accind"/></td>
            <td>AccountNumber:</td><td><input type="text" name="accountNumber" id="accnum"/></td>
            <td><button onclick="createAccount()">Create Account</button></td>

How page looks:


Hope it will help you!


Popular Salesforce Blogs