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...

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 Sales...

Summer 16 Salesforce Administrator Maintenance exams

I was able to clear my Summer 16 Salesforce administrator maintence exam earlier today. Next I need to start working on Winter 17 immediately. Anyway before that i thought i will leave some pointers to any folks who may be taking Summer 16 anytime soon Below are some topics you can read about to prepare yourself for the exam - 1. Process builder improvements -  Process builder can now execute immediate actions and then evaluate the next criteria in the flow. Please see this link - https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_forcecom_process_multiple_actions.htm Expect a question on this. 2. Files sharing permission -  In Classic, when a file is shared to a user via a record, its access can be set to "Set by Record". This allows the record level access to determine access level for the file as well. Read about this below - https://releasenotes.docs.salesforce.com/en-us/summer16/release-notes/rn_files_access_by_record.htm 3. Chatter Q...