You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Dynamically Accessing Salesforce Data Using Site

By: Venkata

Recently, we had a requirement to dynamically access Salesforce data as a JSON response. This can be accomplished by using Rest API and Site. Because 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. Site
Before creating a REST service in Salesforce, first, you need to have a 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, 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 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

References Sites Overview | Adding a Class as a REST Resource | Choosing Between SOQL and SOSL