2015-03-03

One of the first obstacles when learning something new is knowing where or how to start, and we are going to overcome this obstacle using these simple suggestions.

Before approaching this topic, I want to make clear what our starting point is:

1. You do not need to know anything about Salesforce’s internal structure (schema).

2. You do not need to know anything about REST API.

First step: Set up your own playground

The first thing we are going to do is sign up for a Free Developer Environment.

Second step: Learn a little about Salesforce’s internal structure

We are going to use Accounts for our examples, which is a common object used in almost all Salesforce solutions. However, if you want to know more about other objects in Salesforce, take a look at this Data Model documentation.

One way to know more about a particular Salesforce object is by accessing the developer reference, or searching Google with something similar to “Salesforce API Account”, which usually is the fastest way.

The Developer Reference is an excellent tool for knowing object structures in detail, but we are going to obtain this information directly from the Schema through the REST API, which leads us to the next step.

Third step: Ask Salesforce about anything

What better way of learning more about Salesforce than asking them directly? And how can we do that? Simple. The answer is Workbench, a wonderful tool that will allows us to learn our playground in depth, and our main aid during the rest of the guide.

If we go to Workbench and log in using our developer environment’s credentials, we will find a screen like this:



“How is an account record on Salesforce?” will be our first question to Salesforce, so let’s select the following actions in the current screen and then click on the “Select” button:

Jump to: Standard & Custom Objects.

Object: Account.

We will be redirected to the following screen:



From this screen we can obtain all the information we would need: attributes, child relationships, fields and record type information. I doubt you will ever need more than that.

It’s tree organisation makes it easy for us to navigate through the immense amount of data Workbench provides.

Fourth step: Managing Salesforce records using the REST API

Now that we know something about Accounts, we can start retrieving some of them from the database through the REST API. Let’s go to Utilities > REST Explorer.



Ok, if you have not run away after seeing that screen, let’s try to understand what kind of magic runs behind it.

We should probably start with the so called “HTTP methods” since they are the first decision we need to make: GET, POST, PUT, PATCH, DELETE and HEAD.

I am going to explain briefly the main ones:

GET: Lists the records.
POST: Creates a new record.
PUT: Updates a record.
DELETE: Deletes a record.

So, for example, if we want to retrieve a list of Accounts, we will have to use the GET method. Not too difficult is it?

Let’s keep the “GET” method chosen and go for the text input box.

/services/data/v29.0

“What do we have to put in here?” is probably the question running through your mind now. For answering this question we are going to need some help from our friend the Developer Reference, specifically this page https://www.salesforce.com/us/developer/docs/api_rest/Content/resources_list.htm

Here we can find all the basic paths for our common needs. Let’s try to get all the account names.

According to the page, we should use this path “/vXX.X/query/?q=soql” so let’s change it for our purposes:

Change “vXX.X” to “v29.0”, which is the version we are using.

Change “soql” to “SELECT Name FROM Account”. It is pretty obvious what that means.

If we use the path “/services/data/v29.0/query/?q=SELECT+Name+FROM+Account” we will end up with a screen showing something similar to this:

Voila! There are our account records!

Now, let’s create a record.

1. Change the HTTP method to POST.

2. Change the URL to /services/data/v29.0/sobjects/Account/

3. Insert this code in the “Request body” text area:

{

“Name” : “My Account”

}

4. Click the “Execute” button.

Fifth step: Play on your own

I hope you feel like the REST API is no longer an obstacle! Now you know some of the basics this will help you gain a deeper understanding of this useful tool.

Let’s summarise what we heave learnt:

How to obtain a new free developer environment.

How to obtain information about a Salesforce object using the Developer Reference.

How to obtain information about a Salesforce object using Workbench.

What HTTP methods are used for.

How to retrieve object records using REST API.

How to create objects using REST API.

By Samuel Acuna – Senior Salesforce Developer – CloudSocius

Click here for further information on the Salesforce Development services CloudSocius offer.

The post Where To Start Learning About The Salesforce REST API appeared first on CloudSocius.

Show more