SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Monday, 9 December 2019

Salesforce Integration Tutorial Part 9 - Test class for Apex REST Callout

Hello Trailblazers,

Welcome to the 9th tutorial of the Salesforce Integration Tutorial series. In the previous tutorial, we performed a callout to an external REST API and learned about the concept of wrapper classes and remote site settings. In this tutorial, we're going to create a test class for our api callout. I have made a small update in our SFDCStopCallout class in which I returned the response which I got from the API and updated the return type of getBlogs() method to HTTPResponse. You can have a look at the updated SFDCStopCallout class below:-

As shown above, we've the updated class and we're going to create a test class for this API callout. Let's begin.

Create a Mock Class

The first step is to create a mock class for API callout. The mock class is nothing but a class that will generate a fake response for our API callout in the test class. As, we don't need to hit the actual API while testing, we should have a fake response that will be returned when a callout is performed. I created a mock class for my SFDCStop Blogs API named as SFDCStopMock:-

As you can see above, the mock class is also a test class with @isTest annotation that implements HTTPCalloutMock interface. This interface consist of a single method respond() which accepts an instance of HttpRequest class as a parameter and return an instance of HttpResponse which we'll construct inside the method itself. So, we've overriden this respond() method inside which we initialized a new instance of HttpResponse named as response. Then we set the fake response body using the setBody() method and we also set the fake response header with key as Content-Type and value as application/json using the setHeader() method. Finally, we set the status code of 200 for the response using setStatusCode() method. At the end, we returned the fake response.

Test class for API Callout

Now, we have our mock API. So, we're ready to create a test class for our SFDCStopCallout class. I have created a new class named SFDCStopCalloutTest which is given below:-

As you can see in the above test class, I have created some constants that are given below along with their purpose:-

  1. RESPONSE_CODE :- This constant consist of the response code of the response coming from API callout.
  2. RESPONSE_HEADER_KEY :- This constant consist of the key in the response header from API callout.
  3. RESPONSE_HEADER_VALUE :- This constant consist of the value in the response header from API callout.
  4. RESPONSE_BODY :- This constant consist of the body of the response from API callout.

All these constants are used in test methods to verify the response coming from the api callouts in the assert methods. As you can see, I have created two test methods:- testGetBlogs and tesGetBlogsUsingFramework. As both the methods are covering the same getBlogs method of SFDCStopCallout class, we're going to have a look at the testGetBlogs method first which is the standard approach.

Inside this method, I have first setup the mock API using Test.setMock() method in which the first parameter is the interface type which is always:- HTTPCalloutMock.class and the second parameter is the object of our mock class. Therefore, I have passed it as new SFDCStopMock() as my mock class is SFDCStopMock which will return a correct fake response for this callout. In the next line, I am performing the API callout by calling the getBlogs() method from SFDCStopCallout class. This method is returning an instance of HTTPResponse that I am storing in a variable named response. Finally, I am verifying the response code, response header and the response body which is returned by our callout method by using System.assertEquals() method.

You'll get 100% coverage of the SFDCStopCallout class using this testGetBlogs() method alone. Now, one question to consider is:-

Do I need to create mock callout classes for each callout ?

The answer is YES. You need to create a mock for each callout you're perfoming in your apex to have full test coverage of that callout. However, you can use the same callout class by adding some conditions to provide different responses for different callouts. The easy solution to this problem is given in our second method which is testGetBlogsUsingFramework().

testGetBlogsUsingFramework() is making use of HTTPCalloutServiceMock class present in HTTPCalloutFramework to test this callout. HTTPCalloutFramework is a framework that simplifies HTTP Callouts in apex. You can install it from the GitHub repository in one click by clicking on the Deploy to Salesforce button. Click this link to go to the repository and click on the button to install the framework.

Now let's have a look at the second method that's using the framework. In this method, first of all I created an instance of HTTPCalloutServiceMock class named as sfdcStopApiMock. Then I set the response code, response header and the response body of my fake response by passing the constants as the parameters in the the setter methods available in the class. In the Test.setMock() method, I have passed the instance of HTTPCalloutServiceMock i.e. sfdcStopApiMock as the second parameter. The rest of the code is the same as in the first method. You can test the SFDCStopCallout class with this second method alone once you have installed the framework and it'll give you 100% coverage. So, we got to a conclusion that by using the framework,

"We are doing the same work performed by creating a separate mock class that is perfomed by writing 4 lines of code using HTTPCalloutFramework"

And the good thing is, you can reuse the same framework class again and again for different callouts. So, it's a great time saver for any developer. Similar is the case while calling out using the framework. If you want to have a detailed look on the framework you can view another blog post by clicking here.

Tired of reading or just scrolled down, don't worry, you can watch the video too.



That's all for this tutorial. If you liked it, make sure to share it among your network and let me know your feedback in the comments section below.

Happy Trailblazing..!!

Tuesday, 3 December 2019

HTTPCalloutFramework - A light weight framework for Apex HTTP Callouts

Hello Trailblazers,

I recently launched a new framework for simplifying apex HTTP callouts known as HTTPCalloutFramework. I open sourced it on GitHub and it's freely available now. In this post, I am going to talk about the functionality and advantages of this framework.

Overview

HTTPCalloutFramework helps you to simplify your apex HTTP callouts by storing all the metadata required for the callout as a record in a custom metadata named as:- HTTPCalloutConfiguration. This metadata stores the following essential information:-
  1. API Endpoint
  2. Request Method (GET/POST...)
  3. URL Parameters
  4. Header Parameters
  5. Request Body
  6. Request Timeout
This framework also includes two reusable mock classes that can be used while writing test classes for callouts:-
  1. HTTPCalloutServiceMock:- This mock class is used when there is a single callout within the code that's covered by the test method.
  2. HTTPCalloutServiceMultiMock:- This mock class is used when there are multiple callouts within the code that's covered by the test method.
These mock classes consists of constructors and methods that can be used to setup a mock response. For an example implementation you can have a look at HTTPCalloutServiceTest class that is used to cover the code in our service class.

Installation

To install this framework in your org, you need to go to the github repository here. Click on the Deploy to Salesforce button as shown below:-


As you click on that button, you'll be taken to a screen as shown below:-

Choose the correct environment and click on the Login to Salesforce button on the top right side. If you're using this tool for the first time, you'll see a screen asking for necessary permissions for deployment as below:-


Click on allow. You will see another screen as shown below:-


This screen is displaying all the metadata that will be deployed to your org. Click on the Deploy at the top right corner. Clicking on this button will deploy the framework to your org. You'll see the messages on the page on clicking deploy button as shown below:-


As you can see above, the deployment is successfully completed, in case there is an error, it'll be shown and the deployment will fail.

Congratulations..!! You have successfully installed HTTPCalloutFramework in your org

Usage

This metadata is mainly used in a service class known as HTTPCalloutService. While creating an instance of this class you can pass the developer name of custom metadata in the constructor and it will automatically fetch the data from HTTPCalloutConfiguration record and set it in the HTTP request.

Suppose, you need to add a request body yourself each time you're making a request as it's dynamic, you can do that easily by using the getter and setter methods in the service class. For ex:- For request body, we have setRequestBody() and getRequestBody() methods that can be used.

While using this framework You should only focus on the stuff that's dynamic while making a callout as the static data can be stored easily in the custom metadata and will be picked automatically by our service class.

Let's have a look at the example code and learn how by using this framework, we can reduce code. In this example, I am going to connect to another Salesforce Org - Playful Bear Org from my Salesforce Org - Curious Racoon Org and query contacts.

There are better ways to do this like:- Using named credentials etc. However, I am doing it solely by writing code for demo purposes.

So, if you want to connect to a different salesforce environment from apex you'll do something as shown below:-

As you can see above, I have simply created a wrapper for the token response in order to get the access token. The code in line no. 10 to 18 is used to hit the token URL and get the response from it. However, the code in line no. 20 to 26 is used to get the actual response by querying the contact from other org.

The other way around can be by using the HTTPCalloutFramework. Let's have a look at the below code:-

As you can see above, I can get the token very easily by using the code shown in line no. 10 to 11. I can query the contacts from other org and get the response from line no. 13 to 18. I have made two custom metadata records, one for authentication and other for querying as shown below:-



If I don't consider the wrapper, the total no. of lines of code a developer has to write without using the framework:- 16 and while using the framework its:- 8. So, we got to know that you can reduce your code to half along with the additional benefits like:- there will be minimal or no change in code in future as most of the details are stored in the custom metadata itself.

Not only this, if multiple developers are working on integrations to connect with the same systems, they can do the authentication stuff in just two lines of code which is common to all of them, there is no need to create a common method or anything like that. Also, for any change in the headers, or metadata we don't need to go to the code and change that you can simply do that in the custom metadata itself. Therefore, the advantages can be summarized as:-


  1. Lines of code are reduced.
  2. Request metadata can be updated without any change in code.
  3. Code reusability is implemented automatically.
  4. Data from static APIs can be fetched using two lines of code.
  5. Getter and setter methods make it easy for us to update the request before sending.
  6. Mock classes are available for handling single and multiple requests in the code covered by a test method while working on test classes.
  7. Named credentials support.

Even if you're using named credentials for the authentication part, it's easy to setup the request in the custom metadata. Let's have a look at the below code that's using named credentials for authentication and is making the same request:-


As you can see above, I have just given my metadata developer name in the constructor i.e. :- PlayfulBearQueryAPIWithNamedCredentials, updated the request URL with my query and displayed the response in debug. It's working fine. Now let's have a look at the custom metadata record for the same:-


You can see that I have specified the request method as GET and the request timeout in miliseconds. However, you can see that the request URL is:- http://callout:Playful_Bear_2/services/data/v47.0/query/?q=

As it's starting from callout: we got to know that it's using named credentials for authentication. The named credential record for playful bear org is given below:-


So, in this way, using both the named credentials and framework, I can even reduce my code more and perform my callouts more easily.

With this blog, I hope you got an idea of how can the HTTPCalloutFramework helps you in managing your REST callouts from Salesforce and how you can install and use it. There are two mock classes also present in the framework that I wanted to describe about but it'll be a very long post in that case. I recommend you to have a look at the test class for my service class here. In this test class, I have used both the mock classes:- HTTPCalloutServiceMock.cls and HTTPCalloutServiceMultiMock.cls for covering single and multiple callouts in the code covered by test methods. You can use these classes too while creating a test class as these are generic mocks. If you still need an example of using these mocks, let me know in the comments down below.

If you liked this framework, make sure to bookmark it on GitHub and show your support by clicking on the Star button present here. Also, share this post among your network and let me know your feedback in the comments section.

Happy Trailblazing..!!

Salesforce Integration Tutorial Part 8 - Apex REST Callouts

Hello Trailblazers,

Welcome to the 8th tutorial of the Salesforce Integration Tutorial series. In previous tutorials, we created a test class for our custom API in salesforce and we've gone through different methods that are available while creating our custom APIs.

In this tutorial, we're not going to create an API in salesforce instead, we'll be calling out an API hosted on an external system from our salesforce org and will get the response from that API. For this purpose, mainly 3 predefined classes are used:-

  1. HttpRequest:- This class is used to create a new HTTP Request to be sent to the external system. It has methods like:- setMethod(), setHeader(), setBody() which are used to set the parameters of HTTP Request.
  2. HttpResponse:- This class is used to store the HTTP Response which is returned from the external system. It has methods like:- getBody(), getStatus(), getStatusCode() which are used to get the data returned in the response.
  3. Http:- This class is used to send an HTTP request to an external API and get the response from that API. It has just two methods:- send() and toString() out of which send() is mostly used. This method takes an instance of HttpRequest as a parameter and return an instance of HttpResponse in the response.

I have created a custom API and deployed it to Heroku, you can have a look at the same here:- https://sfdcstop.herokuapp.com/blogs

This API is simply returning a response as below:-


As you can see above, the response of this API consists of an author as Rahul Malhotra and a list of blogs written by me in the Salesforce Integration Tutorial Series along with the URL of each blog. Now, our task is to call this API from Salesforce and get the response. For this, we first need to create a wrapper class that can parse this JSON response in apex. Fortunately, we have a great tool available for this and it's free. The tool is called JSON2Apex and is available at:- https://json2apex.herokuapp.com/

In this tool, you just need to paste the json which is returned by the API, specify the wrapper class name and click on the Create Apex button. This will download the zip file in your system which consists of a wrapper class that can be used to parse the input json and a test class for that wrapper class. In our case, I am pasting the json returned by my api below:-


As I clicked on Create Apex button, I got the below apex classes that I created in my salesforce org after minor modifications (Added comments and removed _ from the name of test class. Updated SFDCStopBlogsWrapper_Test to SFDCStopBlogsWrapperTest).

You can see above that the wrapper class SFDCStopBlogsWrapper has two data members:- author of type string and blogs of type List<Blogs> where Blogs is the inner class that consists of id, title, and url as string. It can be used to parse the input json which we got in the response as the json consits of author which is a string and list of blogs that are objects consisting of id, title and url which are again strings. The wrapper class also consists of parse() method that is taking the json string as an input and will return an instance of the same wrapper class. It's using JSON.deserialize() method to deserialize the json string. Similarly, you can have a look at the test class code for this wrapper below:-

As you can see above, it consists of a single testmethod that's parsing the same json string that was used in the input while downloading the auto-generated apex classes. This method is just calling the parse() method of wrapper class as it's the only method in the wrapper class. Then it's checking whether the JSON string is parsed properly or not by confirming that the instance of wrapper class is not null.

To make an HTTP callout to an external API, we need to add an entry to the Remote Site Settings in Salesforce where we need to store the base URL of the api. We need to do this to tell Salesforce that we trust this external API/Endpoint URL and thereby, allowing salesforce to use this endpoint to interact with the external system.

For this, go to setup and search for remote site settings:-


You'll see the below page:-


 Click on New Remote Site and add the following data:-


You can see above that I have added only the base URL i.e. https://sfdcstop.herokuapp.com for the remote site setting and not the actual blogs api url i.e. https://sfdcstop.herokuapp.com/blogs. I have done this so that I don't need to create multiple entries for each API in future as Salesforce will be allowed to interact with anything following this base URL. Click on Save.

You'll see that a new remote site record is created. Now, we're ready to create our callout class that will send a request to the external api, get the response and will use the wrapper class to parse that JSON response. I created a new class named SFDCStopCallout. It's code is given below:-

As you can see above, I have a single method in this class named as getBlogs() which will hit the blogs API and get the response from there. Inside this method, I have created and initialized an instance of Http class and HttpRequest class. I used the instance of HTTPRequest class named as:- request and called it's setEndpoint() method in which I passed the endpoint url of our blogs api. Then I called the setMethod() method of request and passed the value GET as we need to send a GET request to the api endpoint. Finally, I used the send() method from the instance of  Http class that will take request (instance of HttpRequest) as a parameter and return an instance of HttpResponse class. The response is assigned to an instance of HttpResponse class named as response. The send() method will actually send the request to the endpoint URL and get the response from there. Finally, I checked if the response status code is 200 by using the getStatusCode() method of response. If it is, I got the response body which is a json string using getBody() method of response. I used the parse() method from my SFDCStopBlogsWrapper class to parse the json string that I got in the response body.

Finally, I used the author variable of my wrapper class to display the author in logs and then I checked if the blogs variable is not null and not empty. If I have the blogs in the response, I iterated those blogs one by one and displayed the id, title and url of each blog.

You can execute this callout in the developer console by calling the getBlogs() method of SFDCStopCallout class as:- SFDCStopCallout.getBlogs(). Then you can check the debug logs to see the response as given below:-



Tired of reading or just scrolled down, don't worry, you can watch the video too.



That's all for this tutorial. We learned how to make a callout to external API from Salesforce and get the response in apex. We also learned about the concept of wrapper classes and got to know about remote site settings. In case you need to pass the data to the external system, you can either pass it in the URL itself or set the data in the request body using the setBody() method of HttpRequest class. In that case you should also set the appropriate method like:- POST, PUT etc. as GET doesn't support the request body. You can have a look at all the methods of HttpRequest class here.

In the next tutorial, we'll create a test class for this api callout. If you liked this tutorial, make sure to share it in your network and let me know your feedback in the comments below.

Happy Trailblazing..!!