Salesforce is a customer relationship management tool (CRM), which is used to help companies manage and promote the relationship with existing clients, and develop new clients. It runs on cloud, so there is no need to install any software and no hardware is required. It can reduce the development cost and deliver an application in a very short time. Currently, Salesforce is a leading on-demand CRM in the market. Thus, integration with Salesforce is more common when we build sites or systems. This blog outlines the benefits and steps for creating a portlet that integrates with Salesforce in Liferay.
- Salesforce Platform APIs
Salesforce provides programmatic access to your organization’s information using simple, powerful, and secure application programming interfaces.
Salesforce provides a lot of APIs, but in this blog I will use REST API.
- Salesforce REST API
The Force.com REST API lets you integrate with Force.com applications using simple HTTP methods, in either XML or JSON formats, making this an ideal API for developing mobile applications or external clients. Force.com also supports Apex REST, which lets you create Web services on Force.com using Apex.
Below are the benefits of using the REST API:
- Ubiquitous access: Use standard HTTP method call-outs, available in every language and platform, to make requests and retrieve information from Force.com.
- Standards-based security: Utilize the OAuth protocol for authenticating your REST calls.
- Data model: Gain access to the same data model and standard objects as those in SOAP-based Web services.
- Flexible formats: Serialize data in either the XML or JSON format.
The Rest API is based on OAuth. The OAuth refresh token flow is for renewing tokens issued by the web server or user-agent flows. After the consumer has been authorized for access, it can use a refresh token to get a new access token (session ID). This process happens only after the consumer has received a refresh token using either the web server or user-agent flow. It’s up to the consumer to determine when an access token is no longer valid and when to apply for a new one. Bearer flows can be used only after the consumer has received a refresh token.
1.Create a Connected App in Salesforce (In Classic Experience)
Login to your Salesforce org.
Navigate to Create -> Apps.
Click the new button in the Connected Apps section to create a new one.
Fill in the required information in Basic Information
Check Enable OAuth Settings in API (Enable OAuth Settings). Fill in the Callback URL and then select all items in Available OAuth Scopes to Selected OAuth Scopes.
After you click the save button, in the new page you will get the Consumer Key and Consumer Secret that we will use to our Liferay portlet.
2. Get Token in your Liferay portlet.
In your Liferay portlet you will need to write java code to send an HTTP request in order to get the Token. Here is the example. “client_id” is Consumer Key and “client_secret” is the Consumer Secret you get in the first step. You will get the “security_token” by email once you create your Salesforce account.
Below is the response result:
That’s it. The “access_token” is the Token you get from Salesforce.
3.Send a request with the Token to a business entity.
Once you get the Token, you can get the data you want from Salesforce. All you need to do is to search the related REST API and then put it in your java code. Here is the example to get the Leads data in Salesforce. The related REST API is “https://ap5.salesforce.com/services/data/v39.0/sobjects/Lead/”
Below is the shortcut about the response.
Congratulation! You now know the basic process for using REST API to get data from Salesforce. There is no doubt that you wan to know more about REST API. You can see the REST API Developer Guide.