Salesforce Rest API - Create Case

Salesforce Rest API - Create Case

Salesforce provides standard APIs for many of the actions one would need to perform.

  • Request information about the version of the API
  • Get detail information about a Salesforce object like Account, Case or Custom Object
  • Perform CRUD operation on a Salesforce standard or custom object

This makes it easier to integrate with Salesforce with these standard APIs. Let’s see how to create a case using the Rest API.

  1. Get access token – here we’ll use the username-password flow to get the OAuth
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&amp; 
client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82HnFVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&amp; 
client_secret=1955279925675241571&amp;
username=testuser%40salesforce.com&amp; 
password=mypassword123456fgshjdfsdljfhsdhfsdfs

token using Client ID & Secret.

  1. The next step is to use the access token and the instance_url from the response from Step 1
{
  "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.

Get Access Token – Request

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&amp; 
client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82HnFVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&amp; 
client_secret=1955279925675241571&amp;
username=testuser%40salesforce.com&amp; 
password=mypassword123456fgshjdfsdljfhsdhfsdfs

Response

{
  "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="
}

Call the create Case RestAPI with access_token from the previous step

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",
   ...
   ...
}

Response

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

comments powered by Disqus