Salesforce provides standard APIs for many of the actions one would need to perform.
This makes it easier to integrate with Salesforce with these standard APIs. Let’s see how to create a case using the Rest API.
Bearer <access_token>
POST /services/oauth2/token HTTP/1.1
Host: login.salesforce.com X-PrettyPrint: 1
Content-Type: application/x-www-form-urlencoded
grant_type=password&
client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82HnFVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&
client_secret=1955279925675241571&
username=testuser%40salesforce.com&
password=mypassword123456fgshjdfsdljfhsdhfsdfs
token using Client ID & Secret.
{
"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"instance_url": "https://yourdomain.my.salesforce.com",
"id":"https://login.salesforce.com/id/XXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXX",
"token_type": "Bearer",
"issued_at": "1454735969118",
"signature": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="
}
and make the actual RestAPI call to create the case.
Set up a connected app in Salesforce and get the Client ID and Secret. Create an API only user and get the username, password, and security token.
Note: the password in the request includes password & security-token combined.
POST /services/oauth2/token HTTP/1.1
Host: login.salesforce.com X-PrettyPrint: 1
Content-Type: application/x-www-form-urlencoded
grant_type=password&
client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82HnFVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&
client_secret=1955279925675241571&
username=testuser%40salesforce.com&
password=mypassword123456fgshjdfsdljfhsdhfsdfs
{
"access_token":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"instance_url": "https://yourdomain.my.salesforce.com",
"id":"https://login.salesforce.com/id/XXXXXXXXXXXXXXXXXX/XXXXXXXXXXXXXXXXXX",
"token_type": "Bearer",
"issued_at": "1454735969118",
"signature": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX="
}
Use the instance_url and the access_token from the previous step to make the call to create a Case.
Use access_token in Authorization Header as Authorization:
Bearer <access_token>
Use the <instance_url> along with the endpoint to access the intended API.
Create Case RestAPI request
POST services/data/v42.0/sobjects/Case
Host: <instance_url>
Authorization: Bearer <access_token>
Content-Type: application/json
{
"Subject": "subject goes here",
...
...
}
201 Created
{
"id": "a002800000PA0AzAAL",
"success": true,
"errors": []
}
That’s it, you’ve created a case using RestAPI!
Here’s are some useful links,
REST API:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_what_is_rest_api.htm
Authentication:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_authentication.htm
Create Record Example:
(For creating case, replace “Account” with “Case”), use appropriate request body.)
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_create.htm
Case Object & Fields:
https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_case.htm