Dynamically Accessing Salesforce Data Using Force.com Site

Recently, we had a requirement to dynamically access Salesforce data as a JSON response. This can be accomplished by using Rest API and Force.com Site.

Recently, we had a requirement to dynamically access Salesforce data as a JSON response. This can be accomplished by using Rest API and Force.com Site. Because Force.com site is a public website, anyone can access and leverage the data. In general, while accessing Salesforce data by an external site/client, the client needs to get authenticated by Salesforce in order to retrieve a data, but in this approach, the client doesn’t need to get authenticated by Salesforce.

Force.com Site
Before creating a REST service in Salesforce, first, you need to have a Force.com site. If you don’t have a Site, then go to Setup -> Build -> Develop -> Sites to create your unique site.

Once the Apex Class and Site are created, then go to Setup -> Build -> Develop -> Click the Site Label Name -> Public Access Setting -> Enabled Apex Class Access -> Add the Apex Classes you have created.

To get a data from Salesforce, construct and use URL like this:

In the above URL,
yoursalesforcedomain.cs14.force.com is your unique site name.
SearchService is the Rest Resource URL that you have used in your class.
contactName in the name of the parameter and “Test” is value of that parameter.

Here, the name of the parameter is case sensitive. For example, contactName is different from contactname. When the REST service is called through the Site, the service will return a list of records of employers whose name start with “Test”, and these results will be in JSON format. By parsing the JSON response, you can easily display the results to the end user.

Steps to Access Salesforce Data
To dynamically access the salesforce data, follow the below steps:

1. Create a global class with @RestResource annotation. It is used at the class level and enables you to expose an Apex class as a REST resource.
2. Create a method @HttpGet annotation. To use this annotation, your Apex method must be defined as global static and this will expose the method as a REST API that is called when an HTTP GET request is sent from the client.
3. Get the parameter value sent from the URL (e.g.) contactName and store this value in a variable.
4. Query the record values using Salesforce Object Search Language (SOSL), because SOQL supports only one object at a time, but SOSL allows you to efficiently search text, email, and phone fields for multiple objects at a time with a single query.
5. After the SOSL execution, the JSON response should look like below:


The approach explained above is very useful when searching any data from the Force.com site, and the Site will return a JSON formatted string data. By using this JSON data, we can easily display the data to end user in different formats such as table, grid, and etc. So, using Sites, we can eliminate the need for authentication steps in Salesforce.com.

Force.com Sites Overview | Adding a Class as a REST Resource 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.