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 -
Comments
Post a Comment