Wednesday, December 10, 2014

The Ajax Toolkit for Salesforce

Why overload SOQL queries, when you can play with API calls to retrieve, create and delete data using the set of unique features provided by salesforce with AJAX toolkit.
The Ajax toolkit gives anyone familiar with Java script the ability to write the code. It is simple and lightweight. It runs on a browser, which doesn’t require execution of code from Salesforce servers. Ajax toolkit doesn’t affect test coverage. And makes development and deployment quick and easy.
Ajax toolkit does not require a single line of Apex code. Because the Ajax toolkit is making purely API calls, and as Salesforce Professional Edition doesn’t allow API calls, don’t even think about using Ajax tool kit with the Professional Edition.
The Ajax toolkit handles errors easily and its flashing feature also supports parent-child relationship queries. We can query any S object using the Ajax toolkit with the API. Consider the below code:
result = sforce.connection.query(“Select Name, Id from User”);
 records = result.getArray(“records”);for (var i=0; i< records.length; i++) {
var record = records[i];
log(record.Name + ” — ” + record.Id);
}
In the above example, “result = sforce.connection.query” is the parameter which connects to the API to query records. The Salesforce server checks the incoming API from the browser for its IP address. If the IP address is in the trusted IP range, The API is allowed to access the database, else it is bounced back.
We can also access parent child relationship in Salesforce.
//Query the parent child or child parent relationship
var result = sforce.connection.query(“SELECT c.Id, c.firstname, ” +
“c.lastname, c.leadsource, a.Id, a.name, a.industry, c.accountId ” +
“FROM Contact c, c.account a ORDER BY leadsource LIMIT 10″);var it = new sforce.QueryResultIterator(result);

With the Ajax toolkit, you can make synchronous and asynchronous call outs from Salesforce. With Ajax toolkit, you can make your life easier. Give it a try!

Authored by: Nirmal Christopher,
 Salesforce.com Certified Developer, 
Technical Consultant, 
Global Tech & Resources, Inc. (GTR).