Creating an endpoint in Salesforce and using REST API to externally access it

The Salesforce Force.com REST API lets you integrate Salesforce applications using standard HTTP methods. This can then be used to integrate Salesforce with other systems. With help of Apex REST, we can expose the data from Salesforce orgs and we can also fetch data from other systems which hits the particular endpoint (URL).

We will use Salesforce Site as our endpoint where most of the action will happen. For our demo purpose, we will create a Custom object named “Demo”. Once the object is created using the below code.

Creating Demo Object

Apex Class code for Demo Controller

Now, create a site in Salesforce, if you are new and if you need not know how to create a site in Salesforce then refer this link. Once your site is created in its “Public Access Settings” then add the above created Class into “Enabled Apex Classes” and save it. Then at profile level just enable the Read, Create, Edit and Delete access to the “Demo” object. Now, that the configuration and coding are completed, its time to test the REST API.

From your site section select the URL of your newly created site and append it with “services/apexrest/” and URLMapping mentioned in the code at the end. Your URL must look something like this-
https://<>/services/apexrest/Demo/

Now, let’s add a record from an external system to Salesforce using REST API, for this, we will be using Google’s ARC (Advanced REST Client). In the Header section Add content type as JSON and then click add body and use the below JSON as body and use “POST” Method to post data from ARC to Salesforce.
JSON-{
“Name”: “REST Demo”
}

Once you execute the request then you should receive status code 200 denoting success and in response, you should receive the record id of the record just created.

Response that record is created

To verify you can simply use the “GET” method to fetch the data and see for yourself.