Insert Records in SF by using JavaScript without Apex classes or controller.


<apex:page id=”Page” sidebar=”false”>
<script src=”/soap/ajax/20.0/connection.js” type=”text/javascript”></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(“Page:Form:PB:PBS:PBSI:Name”).value;

// This is the line where magic happens, calling Create() method.
var result = sforce.connection.create([account]);

if (result[0].getBoolean(“success”)) {
alert(“New Account is created with id ” + result[0].id);
} else {
alert(“failed to create new Account ” + result[0]);
<apex:form id=”Form”>
<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=”Name” />
<apex:commandButton onclick=”return insertAccount();” value=”Save”/>

Print Friendly, PDF & Email
This entry was posted in Development. Bookmark the permalink.