Activity Forums Salesforce® Discussions How to insert record on sobject using visualforce page without using controller?

  • shradha jain

    Member
    August 21, 2018 at 9:56 am

    Hello Parul,

    You can use remote objects to insert a record on sobject using visualforce page without using controller. Remote Objects are proxy objects that enable basic DML operations on sObjects directly from JavaScript.All of the data access is handled by the Remote Objects components.

    Example:

    This Visualforce page retrieves a list of 10 Warehouse records and displays them on the page in response to the user clicking the Retrieve Warehouses button.

    <apex:page>
    <!-- Remote Objects definition to set accessible sObjects and fields -->
    <apex:remoteObjects >
    <apex:remoteObjectModel name="Warehouse__c" jsShorthand="Warehouse"
    fields="Name,Id">
    <apex:remoteObjectField name="Phone__c" jsShorthand="Phone"/>
    </apex:remoteObjectModel>
    </apex:remoteObjects>

    <!-- JavaScript to make Remote Objects calls -->
    <script>
    var fetchWarehouses = function(){
    // Create a new Remote Object
    var wh = new SObjectModel.Warehouse();

    // Use the Remote Object to query for 10 warehouse records
    wh.retrieve({ limit: 10 }, function(err, records, event){
    if(err) {
    alert(err.message);
    }
    else {
    var ul = document.getElementById("warehousesList");
    records.forEach(function(record) {
    // Build the text for a warehouse line item
    var whText = record.get("Name");
    whText += " -- ";
    whText += record.get("Phone");

    // Add the line item to the warehouses list
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(whText));
    ul.appendChild(li);
    });
    }
    });
    };
    </script>

    <h1>Retrieve Warehouses via Remote Objects</h1>

    <p>Warehouses:</p>

    <ul id="warehousesList">
    </ul>
    <button onclick="fetchWarehouses()">Retrieve Warehouses</button>

    </apex:page>

    You can refer the following link for more information:

    https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_remote_objects.htm

    Thanks.

  • shariq

    Member
    September 16, 2018 at 1:02 pm

    Hi,

    Try this -

    <apex:page id=”pageId”>

    <script src=”/soap/ajax/20.0/connection.js” type=”text/javascript”></script>

    <script>

    function insertAccount(){

    //Getting Session ID.

    sforce.connection.sessionId= “{!$Api.Session_ID}”;

    //Creating New Account Record.

    var account = new sforce.SObject(“Account”);

    //Getting Account Name from inputText.

    account.Name = document.getElementById(“pageId:frm:pb:pbs:pbsi:txtName”).value;

    //Create method

    var result = sforce.connection.create([account]);

    //Getting result

    if(result[0].getBoolean(“success”)) {

    alert(“New Account is created with id ” + result[0].id);

    }

    else{

    alert(“failed to create new Account ” + result[0]);

    }

    }

    </script>

    <apex:form id=”frm”>

    <apex:pageBlock title=”Insert Account” tabStyle=”Account” id=”pb”>

    <apex:pageBlockSection title=”Account Name” columns=”1″ id=”pbs”>

    <apex:pageBlockSectionItem id=”pbsi”>

    <apex:outputLabel value=”Name” />

    <apex:inputText title=”Name” id=”txtName” />

    </apex:pageBlockSectionItem>

    </apex:pageBlockSection>

    <apex:pageBlockButtons>

    <apex:commandButton onclick=”return insertAccount();” value=”Save”/>

    </apex:pageBlockButtons>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

    Hope his helps.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos