Using AJAX Toolkit In Force.com Platform

Force.com platform provides a JavaScript API which provides features to work with sObjects(Standard / Custom) using JavaScript without the help of Apex codes.

When to use ?

Custom Buttons/Links:

Creating a record with pre-populated values, then re-directing the user to this record (in edit mode) after the record is committed to the database.
Updating a value on a related record that may not be accessible via cross-object workflow.
Deleting records related to the current record.

Visualforce Pages:

Creating an “on load” javascript function that runs a DML statement as soon as a Visualforce page is loaded.
Calling a javascript function that contains ajax toolkit functionality via the , or components.

How to use?

1) When using inside a visual force page:
<script src=”/soap/ajax/31.0/connection.js” type=”text/javascript”></script>
<script src=”/soap/ajax/31.0/apex.js” type=”text/javascript”></script>

2) When a custom onclick JavaScript button, we need to specify the login call to log into the API first:

<body>
{!requireScript(“/soap/ajax/31.0/connection.js”)}
sforce.connection.login(“username”, “password”);

API Call Syntax:

Synchronous syntax:

sforce.connection.method(“arg1″,”arg2″, …);

Asynchronous syntax:

sforce.connection.method(“arg1″,”arg2″, …, callback_method);

Fetch Data From sObjects:

var result = sforce.connection.query(“SELECT Id, Name FROM Account LIMIT 1″); //Fetching data from Account object using SOQL.

var records = result.getArray(“records”); // The query result-set is assigned as an array.

var accObj = new sforce.SObject(“Account”); // Creating an instance of the sObject

accObj = records[0];

Inserting the data in sObjects:

var newRecords = new Array();

var accObj = new sforce.SObject(“Account”); // Creating an instance of the sObject

accObj.Name = ‘Mindfire’; //Assigning the Account Name.

newRecords.push(accObj); //Adding records.

sforce.connection.create(newRecords); //Inserting data into the Account object.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

1 Comment

  1. Thanks a lot guys for your contribution to this
    post. It makes it quite better and easier to get.

    I wish all the information would provide as Whoa information.

Leave a Reply

Your email address will not be published. Required fields are marked *