Skip to main content

Integrating Salesforce with a HTTP Rest API service



In one of my previous posts, I created a Rest API endpoint using a Spring based java application and deployed it to Heroku. Now that we have an endpoint that returns json output, I would like to create an integration to it from my Salesforce org.

If you are interested in how I built the rest api end point, please see below steps -

To integrate this in Salesforce, I will create an Apex class to do the callout. The http callout will call the rest api and get the response. For this demo, all i am doing is getting the output and writing it to system debug. But hopefully this will show how the same can be done in a real business usecase.

  • First lets add the API endpoint url as a remote site. This will tell Salesforce that this is a valid endpoint to call. This is done in Setup -> Security Controls -> Remote Site Settings.

  • Now that the site is added, lets create the Apex callout class. I am doing this in my Force IDE - 

  • Now lets build out the callout in the class. Apex has following classes that allow us to make these http calls with appropriate request and response details - Http, HttpRequest, HttpResponse. Please see below example class -

  • Now for testing this, all i am doing is writing out the response body and status into System.debug. In a real integration, you would parse the response and use it. Lets test it out via an Anonymous piece of code below -


  Awesome, as you can see the callout was successful and we got back the json response body. For parsing the json response, there are couple of options. We can either deserialize the response to a class and use that class OR use the JSONParser class methods. I will write a different post showing how this can be done. Till then have fun!

Comments

Popular posts from this blog

Workaround to bypass Salesforce SSO

One of the best practices for implementing Single Sign On for your Salesforce org is always ensure there is a way your System administrator can login via the standard login page side-stepping the SSO configuration.  Reason for this is if ever something goes wrong with your Idp provider's service and the SSO authentication responses are not coming as expected, all your users are unable to login. That is, if you have setup your My domain to prevent logins via standard Salesforce login urls (login.salesforce.com). This includes the System administrator as well. Only if your system administrator can somehow login, then he or she can disable the SSO settings for your domain and allow login via the normal login page as a temporary measure. What do you do in such a situation? Well Salesforce has built a workaround for this which is not well documented anywhere (probably for a good reason :) ). I found out about it from a colleague at work. If your my domain url is - https://Com

Salesforce Big Objects - Key learnings

I remember reading about Salesforce Big Objects before they became available and thought maybe it is an option to backup data present in regular objects. That is, instead of taking a backup via an ETL tool or data loader, maybe this will be an option to backup the data in the Force.com platform itself. Now that it is GA and I am reading more about it, i think the use cases for this are more varied. Some use cases I can think of are –  Archival of key data objects within the platform: You may want to use this option if you dont use any other means to backup key data. Also this may be an attractive option for non-large enterprise customers who dont themselves invest on large data warehouses or data lakes in their enterprise architecture. Ex: customer history (if present in tasks and activities) will become huge over years but this is useful information for reporting & customer analysis. Store key information which is large volume in quantity and also high volume in transa

DBAmp for Salesforce - salesforce integration for SQL Server DBAs

Recently i got the opportunity to explore a tool called DBAmp for integration with Salesforce. I found it to be a very useful tool which will help with any data manipulation requirements in Salesforce. Following are my learnings from the exercise. Hope it helps any of you who may need to work with this tool -  DBAmp is a SQL Server package that can be used to integrate with Salesforce. The site where this software is available is - http://www.forceamp.com/ Overview: It essentially installs on top of an existing SQL Server database and provides an OLE DB connector that can be used to connect to Salesforce. Behind the scenes, it executes API calls against Salesforce for any SQL server command it receives. Thus we can create a connection in SQL server to Salesforce org and pull data into tables in the database just as if we are querying against Salesforce tables directly. Use cases for DBAmap + Salesforce: Many use cases exist for using this tool against Salesforce. Pr