SF Rest API Cheatsheet Web
SF Rest API Cheatsheet Web
Overview
Force.com REST API provides you with a powerful Web services API that you can use to interact with Force.com.
Each resource in REST API is a named URI that’s used with an HTTP method: HEAD, GET, POST, PATCH, or DELETE. All resources are accessed using a
generic interface over HTTP with a base URI that follows your Force.com or Database.com URI. REST API supports authentication using OAuth 2.0 and
supports both JSON and XML (JSON is the default).
Examples
PATCH Updates a record.
Following are some examples of calling REST API using cURL.
Replace token with your session ID. Depending on your platform,
if the token contains an exclamation mark (!), it should be POST Creates a new object.
escaped with a backslash (\!) when used in cURL commands.
Depending on your platform, request headers may require single
DELETE Deletes a record.
quotes (‘) or double quotes (“).
Create an Invoice Statement Execute a Query
cURL Command cURL Command
curl https://instance.salesforce.com/ curl https://instance.salesforce.com/services/
services/data/v24.0/sobjects/Invoice _ data/v24.0/query?q=SELECT+Id,+Name,+Status _ _
Statement _ _ c/ -H "Content-Type:application/ c+FROM+Invoice _ Statement __ c -H “Authorization:
json" -d "{\"Description _ _ c\" : \"New invoice OAuth token” -H “X-PrettyPrint:1”
statement\"}" -H “Authorization: OAuth token" -H
“X-PrettyPrint:1"
Response (JSON)
Response (JSON) {
{ “done” : true,
"id" : "a00D0000008oLd7IAE", “totalSize” : 3,
"errors" : [ ], “records” : [ {
"success" : true “attributes” : {
} “type” : “Invoice_Statement__c”,
“url” : “/services/data/v24.0/sobjects/
Invoice_Statement__c/a00D0000008o6y
3IAA”
},
Update status__c on Invoice_Statement__c “Id” : “a00D0000008o6y3IAA”,
cURL Command “Name” : “INV-0000”,
curl https://instance.salesforce.com/services/ “Status__c” : “Open”
data/v24.0/sobjects/Invoice _ Statement _ _ c/ }, {
a00D0000008o6yDIAQ -H "Authorization: OAuth “attributes” : {
token" -H "Content-Type: application/json" “type” : “Invoice_Statement__c”,
-d "{\"Status _ _ c\" : \"Open\"}" -X PATCH -H “url” : “/services/data/v24.0/sobjects/
"X-PrettyPrint:1" Invoice_Statement__c/a00D0000008o6y
DIAQ”
Response (JSON) },
N/A “Id” : “a00D0000008o6yDIAQ”,
“Name” : “INV-0002”,
“Status__c” : “Closed”
} ]
}
Delete an Invoice Statement
cURL Command
curl https://instance.salesforce.com/services/
data/v24.0/sobjects/Invoice _ Statement _ _ c/ OAuth Username/Password Authentication
a00D0000008oLd7IAE -H "Authorization: OAuth
token" -H "X-PrettyPrint:1" -X DELETE cURL Command
curl https://login.salesforce.com/
Response (JSON) services/oauth2/token -d 'grant _
N/A type=password&client _ id=REMOTE _ ACCESS _
CLIENT _ ID&client _ secret=REMOTE _ ACCESS _
CLIENT _ SECRET&username=user@example.
com&password=********' -H "X-PrettyPrint: 1"
Search for a String
cURL Command Response (JSON)
curl https://instance.salesforce.com/services/ {
data/v24.0/search/?q=FIND+%7Bcountry%7D -H "id" : "https://login.salesforce.com/
"Authorization: OAuth token" -H "X-PrettyPrint:1" id/00D50000000IZ3ZEAW/00550000001fg5OAAQ",
Response (JSON) "issued _ at" : "1322006414073",
[ { "instance _ url" :
"attributes" : { "https://instance.salesforce.com",
"type" : "Invoice _ Statement _ _ c", "signature" : "3/
"url" : "/services/data/v24.0/sobjects/ FtJxcTxvpwJx8Tloc/4oNVz1hhzIuppTCOQdrJSjA=",
Invoice _ Statement _ _ c/a00D0000008o6yDIAQ" "access _ token" : "00D50000000IZ3Z!AQ0AQNag4tr.
}, S15f0LoborG12HIls6eEzIxLafInLZIf6RbFiwV2Pjq6 _
"Id" : "a00D0000008o6yDIAQ" QHB4J64a6vSMS705Y _ sIVE _ lq _ xnLIXBXgOKPEo"
} ] }
Force.com REST API Cheatsheet
Create Multiple Records Update a Record and Get Its Field Values in a
Single Request
cURL Command:
curl https://na1.salesforce.com/services/data/
v34.0/composite/tree/Account/ -H "Authorization: cURL Command:
Bearer token -H "Content-Type: application/json" curl https://na1.salesforce.com/services/data/
-d "@newrecords.json" v34.0/composite/batch/ -H "Authorization: Bearer
token -H "Content-Type: application/json" -d
Example request body newrecords.json for creating four "@batch.json"
new accounts:
{ Request body batch.json file:
"records" :[{ {
"attributes" : {"type" : "Account", "batchRequests" : [
"referenceId" : "ref1"}, {
"name" : "SampleAccount1", "method" : "PATCH",
"phone" : "1111111111", "url" : "v34.0/sobjects/account/
"website" : "www.salesforce1.com", 001D000000K0fXOIAZ",
"numberOfEmployees" : "100", "richInput" : {"Name" : “NewName"}
"type" : "Analyst", },{
"industry" : "Banking" "method" : "GET",
},{ "url" : "v34.0/sobjects/
"attributes" : {"type" : "Account", account/001D000000K0fXOIAZ?fields=Name,
"referenceId" : "ref2"}, BillingPostalCode"
"name" : "SampleAccount2",
"phone" : "2222222222", }]
"website" : "www.salesforce2.com", }
"numberOfEmployees" : "250", Response body after successfully executing the subrequests:
"type" : "Analyst", {
"industry" : "Banking" "hasErrors" : false,
},{ "results" : [{
"attributes" : {“type" : “Account", "statusCode" : 204,
"referenceId" : "ref3"}, "result" : null
"name" : “SampleAccount3", },{
"phone" : “3333333333", "statusCode" : 200,
"website" : “www.salesforce3.com",
"numberOfEmployees" : “52000", "result": {
"type" : “Analyst", "attributes" : {
"industry" : “Banking" "type" : "Account",
},{ "url" : "/services/data/v34.0/
"attributes" : {"type" : “Account", sobjects/Account/001D000000K0fXOIAZ"
"referenceId" : "ref4"}, },
"name" : “SampleAccount4", "Name" : “NewName",
"phone" : “4444444444", "BillingPostalCode" : "94105",
"website" : “www.salesforce4.com", "Id" : "001D000000K0fXOIAZ"
"numberOfEmployees" : “2500", }
"type" : “Analyst", }]
"industry" : “Banking" }
}]
}
Example request body newrecords.json for creating four
new accounts:
{
"hasErrors" : false,
"results" : [{
"referenceId" : "ref1",
"id" : “001D000000K1YFjIAN"
},{
"referenceId" : "ref2",
"id" : “001D000000K1YFkIAN"
},{
"referenceId" : "ref3",
"id" : “003D000000K1YFlIAN"
},{
"referenceId" : "ref4",
"id" : “003D000000K1YFmIAN"
}]
}