Generic Apex Class For Calling External Systems

Generic Apex Class For Calling External Systems

In this blog post, we talk about what is Integration? and we will create a generic apex class for calling external systems

What is Integration?

  • Integration is a process of connecting two or more applications. Enterprise systems use many applications, many or most of which are not designed to work with one another out of the box.
  • Each application can have data, business logic, presentation, and security layers, all of which are possible targets for integration.
  • Salesforce apps are developed using Apex, a strongly typed, object-oriented programming language. Apex allows integration with external REST Web services using callouts.
  • Callouts allow communication with external systems via HTTP. Each callout request is associated with an HTTP method and an endpoint.

Things to Consider Before an Integration ?

1. Document what and how often information needs to move between systems:

  • Set aside the effort to map out what information needs to move from one framework to another.
  • This is required to distinguish the information to adjust, figure out which course the information needs to move and the how frequent types of deliberations that you need to make.

2. Review API limits :

  • Like Salesforce, some outside frameworks have an API that restricts the measure of information it can move in 24 hours.
  • Be set up for these limitations by recording the points of confinement for all frameworks associated with the integration.

3. Identify the type of integration involved: Real-time or batch :

  • Continuous integration occurs when a record is made or refreshed.
  • Batch integration happens at interim’s, which are not prompt. Each integration venture must distinguish and archive the kind of integration required to guarantee the best results.
  • Even though these are the most significant contemplations in an integration venture, a fruitful integration relies upon numerous variables identified with the individual frameworks and the integration goals

What are Web Services?

  • Web service is a standardized medium to propagate communication between the client and server applications on the World Wide Web. Web services provide a common platform that allows multiple applications built on various programming languages to have the ability to communicate with each other.
  • Web services is functionality or code which helps us to do integration. Web services are open standard (XML, SOAP, HTTP, etc.) based web applications that interact with other web applications for the purpose of exchanging data.

Type of Web Service :

There are mainly two types of web services.

  • SOAP web services
  • RESTful web services

What is SOAP API ?

  • Application layer protocol used to exchange structured information between systems
  • It uses a Web Services Description Language (WSDL) file to rigorously define the parameters for accessing data through the API.
  • SOAP API supports XML only.
  • Because SOAP API uses the WSDL file as a formal contract between the API and consumer, it’s great for writing server-to-server integrations.
  • Access to Salesforce data and business logic
  • Handles medium data volumes
  • Updates multiple records with single.

What is WSDL?

WSDL (Web Services Description Language) is an XML document that describes a web service. There are two types of WSDL in  Salesforce:

  • Enterprise WSDL
  • Partner WSDL

Calling REST API Endpoints via HTTP :

In order to develop a callout, different Apex classes are used. These classes are described below. 

HTTP Class:

This class is used to initiate an HTTP message exchange and obtain the response. The method send ( ) is used to send the message.

HTTPRequest Class:

This class is used to create an HTTP request that can then be sent using the HTTP class. Various attributes of the HTTP request can be set, such as the method, endpoint, and header fields. 

HTTPResponse Class: 

This class represents the response received from the HTTP class after a request has been sent. The class has methods for getting various parts of the response, such as getStatusCode ( ) and getBody ( ).

This example makes an HTTP GET request to the external server passed to the getCalloutResponseContents method in the url parameter. This example also accesses the body of the returned response.

public class HttpCalloutSample {

  // Pass in the endpoint to be used using the string url

  public String getCalloutResponseContents(String url) {

    // Instantiate a new http object

    Http h = new Http();

     // Instantiate a new HTTP request, specify the method (GET) as well as the endpoint

    HttpRequest req = new HttpRequest();



    // Send the request, and return a response

    HttpResponse res = h.send(req);

    return res.getBody();



Methods exposed by generic class to call External API :

//Call any api without any request parameter and header parameter

public HttpResponse get(string url)

//Call any api with request parameter and without any header parameter

public HttpResponse get(string url, Map params);

//Call any api with any request parameter and header parameter

public HttpResponse get(string urlornc, Map params,Map headers);

//Call any API to create and update request 

public HttpResponse post(string url,Map headers, Map params);

//Call any delete api

public HttpResponse del(string url,Map headers, Map params);

Here is our sample class which implements the HttpCalloutMock :

public with sharing class GenericRESTMock implements HttpCalloutMock{

    protected Integer code;

    protected String status;

    protected String body;

    protected Map<string , String> responseHeaders;

    public GenericRESTMock(Integer code, String status, String body, Map<String,String> responseHeaders) {

        this.code = code;

        this.status = status;

        this.body = body;

        this.responseHeaders = responseHeaders;


    public HTTPResponse respond(HTTPRequest req) {

        //create fake response

        HttpResponse res = new HttpResponse();

        for (String key : this.responseHeaders.keySet()) {

            res.setHeader(key, this.responseHeaders.get(key));





        return res;



And if we have a class that does an HTTP Callout and we want to write a unit test for it. This is how it is going to look like.

public with sharing class MyServiceAPI {


    public static String getTransactions(String dateFrom) {

            HttpRequest req = new HttpRequest();


            //retrieved from named credentials



            req.setHeader(‘Accept’, ‘application/json’);

            Http h = new Http();

            HTTPResponse res = h.send(req);

            if (res.getStatusCode() == 200) {

                return res.getBody();


        return null;



What are the Benefits of Salesforce Integrations?

Improve Productivity :

  • Quite often your sales reps require data from both systems, say Salesforce and the accounting software Quickbooks.
  • However, time is precious for the sales reps and they would not like to waste their time in switching from one system.
  • Integration between Salesforce and Quickbooks will enable the reps to do their jobs at a faster rate.

Access Data from Different Sources:

  • The Salesforce CRM helps to bring different systems together such as data that resides in Supply Chain, HR, external ERP, or even on-premise software like Microsoft, Oracle, SAP, etc.
  • This enables you to build a comprehensive customer view. Salesforce comes with powerful APIs and tools to help in the reduction of integration time.

Make Better Decisions:

  • Better decision making is not possible when the data exists in siloes.
  • Salesforce Integration offers a complete view of the data and that results in improved decision making based on a proper understanding of risks and benefits.
  • Salesforce Integration offers streamlined data flow to improve your decision making with critical insights on customer data.

Automate workflows :

  • Salesforce Integration is beneficial since it automates many workflows for your organization.
  • Usually, these tasks are repetitive and require no human being to handle this and automation is the right answer

Modernize the Data Infrastructure :

  • The enterprises must get the right insights for informed decision making.
  • Salesforce Integration extends help to your business with the modernization of your infrastructure and extracts the best out of your customer data.