sfdc rest api

46
Author Audition @patlatus www.patlatus.wordpress.com SALESFORCE DEVELOPMENT TEAM LEAD SALESFORCE CERTIFIED FORCE.COM ADVANCED DEVELOPER Bohdan Dovhan Salesforce REST API: Remote SOQL, SOSL, CRUD and other available actions Headshot optional

Upload: bohdan-dovhan

Post on 14-Jan-2017

473 views

Category:

Self Improvement


4 download

TRANSCRIPT

Page 1: SFDC REST API

Author Audition

@patlatus www.patlatus.wordpress.com

S A L E S F O RC E D E V E LO P M E N T T E A M L E A D S A L E S F O RC E C E RT I F I E D F O RC E . C O M A D VA N C E D D E V E LO P E R

Bohdan Dovhan

Salesforce REST API:Remote SOQL, SOSL, CRUD and other avai lable act ions

Headshot optional

Page 2: SFDC REST API

Author Audition

 REST Design Salesforce REST API REST API Demo Application Knowledge of REST will help to understand how web works and knowledge of REST API will help to implement custom integration to Salesforce

Overview

Page 3: SFDC REST API

Author Audition

Representational state transfer

Representational state transfer is the software architectural style of the World Wide Web. The purpose of REST architecture is to induce

* Performance * Scalability* Simplicity* Modifiability* Visibility* Portability* Reliability

Page 4: SFDC REST API

Author Audition

Roy Fielding coined the term

The term representational state transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation at UC Irvine. REST has been applied to describe desired web architecture, to identify existing problems, to compare alternative solutions and to ensure that protocol extensions would not violate the core constraints that make the web successful. Fielding used REST to design HTTP 1.1 and Uniform Resource Identifiers (URI).

Page 5: SFDC REST API

Author Audition

RESTful systems

To the extent that systems conform to the constraints of REST they can be called RESTful. RESTful systems typically, but not always, communicate over Hypertext Transfer Protocol (HTTP) with the same HTTP verbs (GET, POST, PUT, DELETE, PATCH ) that web browsers use to retrieve web pages and to send data to remote servers. REST systems interface with external systems as web resources identified by Uniform Resource Identifiers (URIs), for example /people/tom, which can be operated upon using standard verbs such as GET /people/tom.

Page 6: SFDC REST API

Author Audition

Examples

* Google Glass API* Twitter API* Amazon Web Services* Atom (RESTful alternative to RSS)* Tesla Model S uses RESTful calls to communicate between mobile devices and car: http://docs.timdorr.apiary.io/

Page 7: SFDC REST API

Author Audition

Understanding Force.com REST Resources

A REST resource is an abstraction of a piece of information, such as a single data record, a collection of records, or even dynamic real-time information. Each resource in the Force.com REST API is identified by a named URI, and is accessed using standard HTTP methods (HEAD, GET, POST, PATCH, DELETE). The Force.com REST API is based on the usage of resources, their URIs, and the links between them.

NOTA BENE: no “PUT” verb. PUT was used to replace the entire resource, not used in Force.com REST API

Page 8: SFDC REST API

Author Audition

Understanding Force.com REST Resources

You use a resource to interact with your Salesforce or Force.com organization. For example, you can:

Retrieve summary information about the API versions available to you.Obtain detailed information about a Salesforce object such as an Account or a custom object.Obtain detailed information about Force.com objects, such as User or a custom object.Perform a query or search.Update or delete records.

Page 9: SFDC REST API

Author Audition

What is the difference between HEAD and GET?

The HTTP methods are used to indicate the desired action, such as retrieving information, as well as creating, updating, and deleting records.

• HEAD is used to retrieve resource metadata. The same as GET but lacks resp. body• GET is used to retrieve information, such as basic resource summary information.• POST is used to create a new object.• PATCH is used to update a record.• DELETE is used to delete a record.

Page 10: SFDC REST API

Author Audition

REST Principles: Stateless and Caching

Stateless

Each request from client to server must contain all the information necessary to understand the request, and not use any stored context on the server. However, the representations of the resources are interconnected using URLs, which allow the client to progress between states.

Caching behavior

Responses are labeled as cacheable or non-cacheable.

Page 11: SFDC REST API

Author Audition

REST Principles: Uniformity and Naming

Uniform interface

All resources are accessed with a generic interface over HTTP.

Named resources

All resources are named using a base URI that follows your Force.com URI.

Page 12: SFDC REST API

Author Audition

REST Principles: Layers and Authentication

Layered components

The Force.com REST API architecture allows for the existence of such intermediaries as proxy servers and gateways to exist between the client and the resources.

Authentication

The Force.com REST API supports OAuth 2.0 (an open protocol to allow secure API authorization).

Page 13: SFDC REST API

Author Audition

JSON vs. XML

Support for JSON and XML

JSON is the default. You can use the HTTP ACCEPT header to select either JSON or XML, or append .json or .xml to the URI (for example, /Account/001D000000INjVe.json).

The JavaScript Object Notation (JSON) format is supported with UTF-8. Date-time information is in ISO8601 format.

XML serialization is similar to SOAP API. XML requests are supported in UTF-8 and UTF-16, and XML responses are provided in UTF-8.

Page 14: SFDC REST API

Author Audition

Relationship URLs a.k.a. “Friendly”

Why make two API calls when you can make just one? A friendly URL provides an intuitive way to construct REST API requests and minimizes the number of round-trips between your app and Salesforce org. Friendly URLs are available in API version 36.0 and later. This functionality is exposed via the SObject Relationships resource. Accessing a contact’s parent account without a friendly URL involves requesting the contact record using the SObject Rows resource. Then you examine the account relationship field to obtain the account ID and request the account record with another call to SObject Rows. Using a friendly URL, you can access the account in a single call directly from the contact’s path: /services/data/v36.0/sobjects/contact/id/account.

Page 15: SFDC REST API

Author Audition

REST API vs. SOAP API vs. Bulk API

SOAP API may be more convenient to process multiple records ( it has the same method for one or multiple records DML operation while REST API has different resource for multiple records DML operation /composite/tree/ )

If you need to process huge amount of data, use Bulk API

While it is possible to query or search for multiple records in REST API using one request, to perform Update\Delete operations you need to perform one request per each record or use /composite/batch/ to unite DML operations in a batch

Page 16: SFDC REST API

Author Audition

How can we know available versions?

Versions resource. URI: /

Formats: JSON, XML; HTTP Method: GET; Authentication: none; Parameters: none

Lists summary information about each Salesforce version currently available, including the version, label, and a link to each version's root.

http://login.salesforce.com/services/data/

http://login.salesforce.com/services/data/v37.0 Is Summer’16 is on your production?

Page 17: SFDC REST API

Author Audition

Page 18: SFDC REST API

Author Audition

List Available REST Resources

Page 19: SFDC REST API

Author Audition

Page 20: SFDC REST API

Author Audition

Get a List of Objects

Page 21: SFDC REST API

Author Audition

Page 22: SFDC REST API

Author Audition

Get Field and Other Metadata for an Object

Page 23: SFDC REST API

Author Audition

Get Field and Other Metadata for an Object

Page 24: SFDC REST API

Author Audition

Running SOQL query

select Id, Name from Organization

Page 25: SFDC REST API

Author Audition

select Id, Name from ApexClass

Page 26: SFDC REST API

Author Audition

Running SOSL search

FIND {REST API DEMO} RETURNING ApexClass (Id, Name), ApexPage (Id, Name)

Page 27: SFDC REST API

Author Audition

find {oil} returning account(id,name), opportunity(id,name)

find {oil} returning account(id,name), opportunity(id,name)

Page 28: SFDC REST API

Author Audition

Read record from another Organization

Page 29: SFDC REST API

Author Audition

CRUD: Create using JSON Data

Page 30: SFDC REST API

Author Audition

CRUD: Create using convenient interface

Page 31: SFDC REST API

Author Audition

CRUD: Read

Page 32: SFDC REST API

Author Audition

CRUD: Read using convenient interface

Page 33: SFDC REST API

Author Audition

Certain objects do not allow DML in Apex

Organization o = [ select Id, Name from Organization ];o.Name += 'x';update o;

yields: Line: 3, Column: 1 DML not allowed on Organization

However, some of them allow REST API Update operations

Page 34: SFDC REST API

Author Audition

CRUD: Update using JSON Data

Page 35: SFDC REST API

Author Audition

CRUD: Update using convenient interface

Page 36: SFDC REST API

Author Audition

CRUD: Delete

Page 37: SFDC REST API

Author Audition

CRUD: Delete using convenient interface

Page 38: SFDC REST API

Author Audition

CRUD: Error Handling

Page 39: SFDC REST API

Author Audition

Access to custom REST Servicesrel=/services/apexrest/AccoutEnhanced?name=oil

Page 40: SFDC REST API

Author Audition

Access to custom REST Services

Page 41: SFDC REST API

Author Audition

References

1. http://en.wikipedia.org/wiki/REST2. http://docs.timdorr.apiary.io/#3. http://www.slideshare.net/alexeiskachykhin/representational-state-transfer-36518469

4. http://www.slideshare.net/AshishGore3/dt-meetup-django-rest-framework-vs-tasty-pie

5. https://habrahabr.ru/post/38730/6. https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/

Page 42: SFDC REST API

Author Audition

 Now you should have deeper understanding of what REST is and which systems can be called RESTful Also you should understand how Salesforce REST API works and which abilities it provides You can now use Salesforce REST API in your integration

Summary

Page 43: SFDC REST API

Author Audition

Q & A? Questions?

Page 44: SFDC REST API

Author Audition

Page 45: SFDC REST API

Author Audition

Page 46: SFDC REST API

Author Audition

AND FINALLY: MAY BE THE FORCE.COM WITH YOU...