Keywords

.NET (3) .rb (1) *.cod (1) 3110c (1) Algorithm (1) Amazon Cloud Drive (1) amkette (1) Android (1) Apex (6) apex:dynamic (1) API (1) API version (1) Application Development Contest (2) Artificial Intelligence (2) Atricore (1) b2g (1) Binary Search Tree (1) Blackberry Application Development (1) Blackberry Java Development Environment (1) Blender Game Engine (1) bluetooth (2) Boot2Gecko (1) bug fix (1) C (1) C++ (2) Cloud computing (1) Cloud Storage (1) Code Blocks (1) Code for a Cause (2) codejam (1) Coding (1) const_cast (1) Custom Help (1) Dancing With the Googlers (1) Data Structures (1) desktop environment (5) Doubly Linked List (1) Dropbox (1) dynamic visualforce component (1) dynamic_cast (1) Enterprise WSDL (1) Execution Context (1) fedora 14 (1) fedora 17 (5) Firefox OS (1) Flashing Nokia 3110c handset (1) Force.com (7) Gaia (1) Game Developement (1) GCC (2) GDG (2) Goank (1) Google (4) Google Developer Group (2) Google Drive (1) GTK+ (5) HACK2012 (2) Hall of Mirrors (1) help for this page (1) HTML5 (2) HTTP Web Server (1) IDE (1) Identity Provider (1) Intelligent Systems (1) Java (1) JDE (1) JOSSO (1) location based social network (1) me.social (1) MinGW (1) Natural Language Processing (1) Natural Language Toolkit (1) neckphone (1) NLKT (1) Nokia Pheonix (1) Notebook (1) Numeric XML Tags (1) OAuth2.0 (1) OLPC (7) OLPC-XO-1 (7) One Laptop per Child (5) Override custom help (1) Paas (1) Partner WSDL (1) Polymorphism (1) programming contest (1) PyGTK (4) Python (10) Recycled Numbers (1) reinterpret_cast (1) Research (1) REST (1) RM-237 (1) Robotics (1) Ruby (1) Saas (2) Salesforce.com (7) SDK (1) Service Provider (1) Single sign on (1) SOAP (3) Speaking in Tongues (1) SSO Agent (1) SSO Gateway (1) static_const (1) sugar (7) sugar activity (4) sugarlabs (7) SVG (2) Symbiotic AI (1) Tabbed container (1) TCP/IP (1) TCP/IP stack (1) Typecasting (1) typeid (1) ubuntu 13.10 (1) UDP (1) Upgrade Assembly (1) Visualforce (2) Web Server (1) Web Services (3) Web2.0 (1) wikipedia (1) wikipediaHI (1) WSDL (1) XML tags (1)

Sunday, May 27, 2012

Create RESTful web services on Salesforce.com's Force.com platform through Apex


Introduction

Force.com has released its REST API as a lightweight channel to access data on the platform(cloud). The basic framework remains the same for REST based services. Request and response can flow in XML or JSON format. The beauty of RESTful service is that its very lightweight and easy to use as compared to SOAP based services.

We will quickly jump over to code that explains how to create a RESTful service on Force.com using Apex.


Diving deep into code:
  1. We need a class with keyword "@RestResource" to signify the class responsible for handling HTTP requests and act as REST based web service. Make sure the class has global access specifier.
  2. The urlMapping  property allows us to set path where the service will be available. This is process of setting up the endpoint for service. Example:                                                                             https://.salesforce.com/services/apexrest/showAccounts
  3. Include @HttpGet keyword before method name for denoting a method to respond on a HTTP GET request; include @HttpPost  keyword before method name for denoting a method to respond on a HTTP POST; include @HttpDelete keyword before method name for denoting a method to respond on a HTTP DELETE. Similarly for HTTP PUT and PATCH methods
  4. Make sure that methods responsible for handling HTTP requests have global access specifier and static keyword with them.
  5. The awesome feature is that you can set return type as primitive type, standard objects or custom objects within your salesforce.com development environment. In this example the list of accounts will be returned to client invoking the service.
  6. In order to pass parameters to service, you can use RestRequest object in a method. example: 
  7. The limitation here is that within a RESTful apex class you can have only one method with @HttpGet keyword.i.e. one method can respond to HTTP GET requests in this class or URL path. same applies for @HttpPost, @HttpDelete, @HttpPut, @HttpPatch

Live Action:
In order to consume this service we need a client that can make HTTP calls to the respective endpoint where service is listening for requests. You can write your client using cURL, java, c, c++,etc

The main step to consume this service is to go through OAuth 2.0 authorization flow to access the resources protected by Force.com platform.


For ease I will use Advanced REST client for Chrome to demo this:

Set Endpoint:
I will make a HTTP POST request to endpoint:https://cs5.salesforce.com/services/oauth2/token*When trying this make sure you replace cs5 with appropriate salesforce.com instance


Set body :
grant_type=password&client_id=your_clientid_here&client_secret=your_client_secret_here&username=your_username&password=your_password_here  *Append your security token if your IP, from where you are invoking the serviceis not in IP whitelist.


Response you get:


{"id""https://.salesforce.com/id/00DO00000004qZHMAY/00590000000G0keAAC","issued_at""1338103748715","instance_url""","signature""N++wJCOHvBdyxR9TTa8VpLkRdBGODgvf5VLmgNhdFHQ=","access_token":"00DO00000004qZH!ARYAQD9cEL5NSrHrjHFwuQcSh5ktdbGLobvF8NiasAuYwZ7sm80G48Mt9sdCPsT42_Ff69ieOe9dozP_BYEASuvA_BTPwgP_"}

Screenshot:

Keep this access_token as you need it to invoke the RESTful service.

Invoking the service:
Now we are ready to invoke the service we just created.


Set Endpoint:
I will make a HTTP GET request to endpoint(since method is defined with keyword @HttpGet):

https://cs5.salesforce.com/services/apexrest/showAccounts

*When trying this make sure you replace cs5 with appropriate salesforce.com instance

Set Header:
Authorization: OAuth 00DO00000004qZH!ARYAQH.xMKtnnchDH6zTYvkMMYRLIflZgGiZ74EXgTsRyowAUwyk5Xcc1ZoH3C.sZ4oMnsfioPFbKdhlTSdI02hjQPF2FX_0

Response you get:
[
{
"attributes"{
"type""Account",
"url""/services/data/v25.0/sobjects/Account/001O00000056mOkIAI"
},
"Name""Google",
"Id""001O00000056mOkIAI"
},
{
"attributes"{
"type""Account",
"url""/services/data/v25.0/sobjects/Account/001O0000003oCQnIAM"
},
"Name""Microsoft",
"Id""001O0000003oCQnIAM"
},
{
"attributes"{
"type""Account",
"url""/services/data/v25.0/sobjects/Account/001O00000056mOpIAI"
},
"Name""Facebook",
"Id""001O00000056mOpIAI"
},
{
"attributes"{
"type""Account",
"url""/services/data/v25.0/sobjects/Account/001O00000056mO2IAI"
},
"Name""FourSquare",
"Id""001O00000056mO2IAI"
}
]

screenshot:


This shows the list of accounts in your environment.

I hope this blog help you understand and create RESTful services in Force.com.


Cheers !