Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/switch/field_switch.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/switch/field_switch.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/media/field_media.php on line 46

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/media/field_media.php on line 46

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/select/field_select.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/select/field_select.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/editor/field_editor.php on line 46

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/editor/field_editor.php on line 46

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/text/field_text.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/text/field_text.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/slider/field_slider.php on line 40

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/slider/field_slider.php on line 40

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/radio/field_radio.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/radio/field_radio.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/info/field_info.php on line 45

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/info/field_info.php on line 45

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/typography/field_typography.php on line 50

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/typography/field_typography.php on line 50
How to make HTTP Callouts in salesforce and how to test HTTP Callouts? - Salesforce Next Gen
Http Callout

How to make HTTP Callouts in salesforce and how to test HTTP Callouts?

How to make HTTP Callouts in salesforce and how to test HTTP Callouts?

Salesforce has created three classes to perfom HTTP Callouts namely, Http, HttpRequest, HttpResponse classes.

How to make Http Callout n Salesforce

Http: to initiate the Request and response of, HttpRequest class creates a request object for methods like GET,POST etc and HttpResponse class creates a response object returned from the callout.

Before making Http callout we need to understand that, the response of web callout from the system is dependent on the external web server is therefore advised that web callout should be performed asynchronously, i.e. use @future Annotation, otherwise no further processing before we receive a response.

In below example we will make a GET Http Request to an external web server

Public class HTTPExample {

Public String getHttpResponseFromWebServer(string url){

Http http = new Http();

HttpRequest  req = new HttpRequest();

req.setEndpoint(url);

req.setMethod(‘GET’);

HttpResponse res = http.send(req);

Return res.getBody();

}

}

This is a very basic example of making Http Callout.

Creating Http Callout is easy task, but to deploy code to production we need to cover 75% of the code lines. But test method do not support Http Callouts, then how do we test a class making Http Callout. To overcome this Salesforce has provided with a trick where we provide the test class with a mock response and we use these response to verify the callout class.

To create a class which generates a mock Http callout reponse has to implement HttpCalloutMock Interface. E.g.

Global class mockClassName implements HttpCallMock{

Global HTTPResponse respond(HttpRequest req){

//Write your logic here to create a mock / fake response

}

}

Once we have created our mock callout class we need to call it during the execution of test class. For this we have to call a method Test.setMock and its syntax is as following.

Test.setMock(HttpCalloutMock.class, new mockClassName ());

After this method is called in test class, if an Http request is invoked then no callout is made but the response generated from your mock class’s method is used

Below is a full example of Http callout and how to test it.

@isTest

global class yourMockHttpResponseGeneratorClass implements HttpCalloutMock {

global HTTPResponse respond(HTTPRequest req) {

System.assertEquals(‘http://abc.com/abc’, req.getEndpoint());

System.assertEquals(‘GET’, req.getMethod());

HttpResponse res = new HttpResponse();

res.setHeader(‘Content-Type’, ‘application/json’);

res.setBody(‘{“example”:”test”}’);

res.setStatusCode(200);

return res;

}

}

public class YouClassToCallout {

public static HttpResponse getInfoFromWebService() {

HttpRequest req = new HttpRequest();

req.setEndpoint(‘http://abc.com/abc’);

req.setMethod(‘GET’);

Http http = new Http();

HttpResponse res = http.send(req);

return res;

}

}

@isTest

private class YouClassToCalloutTest {

@isTest static void testingTheCallout() {

// Set your mock response callout class here

Test.setMock(HttpCalloutMock.class, new yourMockHttpResponseGeneratorClass ());

HttpResponse res = YouClassToCallout.getInfoFromWebService ();

// you can Verify the fake response received

String contentType = res.getHeader(‘Content-Type’);

System.assert(contentType == ‘application/json’);

String actualValue = res.getBody();

String expectedValue = ‘{“example”:”test”}’;

System.assertEquals(actualValue, expectedValue);

System.assertEquals(200, res.getStatusCode());

}

}

This is how you make a Http Callout and test it to deploy to production.

Also, Have a look at the below resources:

  1. Best Salesforce Interview Questions book with Apex and Visualforce concept explained

Also, Have a look at the below learning resources:

  1. SOQL (Salesforce Object Query Language)

  2. Apex Trigger Best Practices and the Trigger Framework

  3. Salesforce Interview Question and Answers Part 2

  4. Salesforce Interview Questions on Test Class

  5. Salesforce-lightning-interview-questions-2018

     6. Salesforce Interview Questions Batch Class 

Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 5 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

Leave a Comment

Your email address will not be published.