Although there are now many, many ways to interact with from JavaScript, the JavaScript REST Toolkit (or ‘ForceTK’), first released in March 2011, was one of the pioneers, and remains one of the most popular. ForceTK provides a fairly minimal JavaScript wrapper for the REST API, with methods to create, retrieve, update and delete records, freeing the developer from the chore of constructing resource URLs and making HTTP calls.

The Salesforce1 Platform (comprising, its REST API and much more) is anything but static, however, and ForceTK has just undergone a minor refit to bring it up to date. Let’s take a look at three changes in the current version.

One of the main benefits of ForceTK is that it provides the same interface for accessing the REST API from JavaScript running in Visualforce, on external servers, and in hybrid mobile apps. Part of this abstraction is routing calls via a proxy where the browser’s Same Origin Policy prohibits direct calls to the REST API endpoint. From Spring ’13 on, the endpoint (/services/data) is exposed on the Visualforce servers, so the proxy is not required. The library will automatically call the REST API directly in this case. Note that Apex REST methods (/services/apexrest) still require the proxy.

A downside of using the REST API is that API calls are a limited resource. RemoteTK was written to allow JavaScript on Visualforce pages to avoid consuming API calls by using JavaScript Remoting, but came with a trade off – since calls were routed via an Apex controller, they operated with system-level privileges. Visualforce Remote Objects, released in Winter ’15, provide a much better mechanism, since they correctly respect user-level sharing rules and field level security. RemoteTK has been removed from the toolkit, and developers should migrate their code to Visualforce Remote Objects.

One final addition is the ability to insert and update Blob data via multipart messages – a REST API feature currently available through a pilot program. It is possible to insert or update blob data using a non-multipart message (by passing base64-encoded data in a JSON or XML-encoded message), but if you do, you are limited to 50 MB of text data or 37.5 MB of base64–encoded data. Using multipart messages, you can upload files of any type with a size of up to 500 MB. The new createBlob and updateBlob functions allow creation of ContentVersion records and creation and update of Document records. Here’s a minimal example that shows how it works:

If you’re using ForceTK, grab the new version and let me know what you think in the comments.

Get the latest Salesforce Developer blog posts and podcast episodes via Slack or RSS.

Add to Slack Subscribe to RSS