Activity › Forums › Salesforce® Discussions › How to insert a record in Salesforce using Javascript?
Tagged: AJAX Toolkit, API Methods, Custom Object, Javascript, Salesforce Record, Session Id, sObject
-
How to insert a record in Salesforce using Javascript?
Posted by Parul on August 17, 2018 at 10:22 AMHow to insert a record in Salesforce using Javascript?
shariq replied 7 years, 8 months ago 3 Members · 2 Replies -
2 Replies
-
Hi Parul,
These are the steps to insert data using Javascript:
- Connecting to the AJAX Toolkit (By using login methods or getting Session_ID).
- Embedding the API methods in JavaScript.
- Processing the results.
Example –
var item = new sforce.SOBject(‘CustomObject__c’);
item.Name = ‘Hello World’;
item.CustomField__c = new Date();
sforce.connection.insert([item],{onSuccess:alert, onFailure:alert});For more info:- Read here
Thanks.
- [adinserter block='9']
-
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 this helps.
Log In to reply.