Salesforce Integration Basics

salesforce integration

Salesforce Integration Basics 

What's up folks !! looking for a fresh start to your salesforce integration journey? Then you are at right place.
Before starting anything you need to understand all basic concepts of salesforce integration. Integration is always considered as the most complex part of your projects as you are connecting two different systems together. Just like a bridge which connects two different cities. So let's start with very basics of this integration with a very simple explanation of concepts.   
So let's get started...

HTTP: Hyper Text Transfer Protocol 

This protocol is used to communicate between two servers or Applications. Salesforce has defined three classes to handle Request and Response.
1. Http
2. HttpRequest
3. HttpResponse 

1. Http Class :
This class contains the functionality to handle the request and response functionality. These classes have two methods.

1. send(HttpRequest request) :
This method is used to send the Sends an HttpRequest and return the response.

SYNTAX :
--------------------------------------------------------------
HttpResponse send(HttpRequest request)
--------------------------------------------------------------

2. toString() :
This method returns a string that displays and identifies the object's properties.

SYNTAX :
--------------------------------------------------------------
public String toString()
--------------------------------------------------------------

2. HttpRequest Class :
This class is used to create HttpRequest programmatically

SYNTAX :
--------------------------------------------------------------
HttpRequest request = new HttpRequest();
--------------------------------------------------------------
Now there are some method of HttpRequest class which we can use, let's understand those methods one by one.

1. SetEndPoint(endpoint url) :
This method is used to set the endpoint URL to which we are requesting 

SYNTAX : 
--------------------------------------------------------------
Public void setEndPoint(EndPoint)
--------------------------------------------------------------

2. getEndPoint() :
This method will return the Endpoint URL of the external server to which we have sent a request.

SYNTAX :
--------------------------------------------------------------
String getEndPoint()
--------------------------------------------------------------

For Example :
--------------------------------------------------------------
HttpRequest request = new HttpRequest();
request.setEndPoint('http://www.salesforcekid.com');
String endPoint = request.getEndPoint();
System.debug(endpoint); //http://www.salesforcekid.com
--------------------------------------------------------------

3. setMethod() :
This method will be used to specify the type of method to be used for the HTTP request. This is method is very important as it decides the what kind of operations you want to perform with data. 

SYNTAX :
--------------------------------------------------------------
public void setMethod(String)
--------------------------------------------------------------
Possible values for the method type include :
1. GET 
2. HEAD
3. POST
4. PUT 
5. DELETE 
6. CONNECT 
7. OPTIONS
8. TRACE

1. GET : 
The GET method is used to retrieve information from the given server using a given URL. Request using GET should only retrieve data and should have no other effect on the data.

2. HEAD :
Same as GET, but transfers the status line and header sections only.

3. POST : 
A POST request is used to send data to the server. For example, customer information, file upload, etc. using HTML forms. 

4. PUT :
Replaces all current representations of the target resource with the uploaded content.

5. DELETE :
Removes all current representations of the target resource given by a URL.

6. CONNECT :
Establishes a tunnel to the server identified by a given URL.

7. OPTIONS :
Describes the communication options for the target resource.

8. TRACE :
Performs a message loop-back test along the path to the target resource.

Now it's time to understand the implementation of these methods one by one in a very short time. So let's begin...

4. getMethod() :
This method will return the type of method used in the HttpRequest 

SYNTAX :
------------------------------------------------------------
public String getMethod() :
------------------------------------------------------------

EXAMPLE :
------------------------------------------------------------
HttpRequest request = new HttpRequest();
request.setMethod('GET');
String methodType = request.getMethod();
System.debug(methodType); //GET
------------------------------------------------------------

5. setBody() :
This method is used to set the body of the HttpRequest 

SYNTAX :
------------------------------------------------------------
public void setBody(String Body)
------------------------------------------------------------

6. getBody() :
This method is used to get the body of the HttpRequest what we have to endpoint URL 

SYNTAX :
------------------------------------------------------------
public String getBody()
------------------------------------------------------------

EXAMPLE :
------------------------------------------------------------
HttpRequest request = new HttpRequest();
request.setBody('This is body');
String body = request.getBody();
System.debug(body); //This is body 
------------------------------------------------------------

7. setBodyAsBlob(Blob body);
This method is used to set the Blob content as body of the HttpRequest 

SYNTAX :
------------------------------------------------------------
public Blob getBodyAsBlob(Blob body)
------------------------------------------------------------

8. getBodyAsBlob() :
This method will return the body of the HttpRequest in the form as a BLOB 

SYNTAX :
------------------------------------------------------------
HttpRequest request = new HttpRequest();
Blob body = Blob.toPdf('This is sample');
request.setBodyAsBlob(body);
Blob b1 = request.getBodyAsBlob();
System.debug(b1);
------------------------------------------------------------

9. setDocumentBody() :
This method is used to set the DOM.Document object as Body, i.e. sending XML document As DOM.Document

SYNTAX :
------------------------------------------------------------
public void setDocumentBody()
------------------------------------------------------------

10. getDocumentBody();
This method is used to get the DOM.Document object which is set in HttpRequest

SYNTAX :
------------------------------------------------------------
DOM.DocumentgetDocumentBody()
------------------------------------------------------------

EXAMPLE :
------------------------------------------------------------
HttpRequest request = new HttpRequest();
String xmldoc = '<NAME>SalesforceKid</NAME>';
DOM.Document doc = new DOM.Document();
doc.load(xmldoc);
request.setBodyDocument(doc);
DOM.Document result = request.getBodyDocument();
System.debug(result.toXMLString()); //<? Xml version='1.0'><Name>SalesforceKid</Name>
------------------------------------------------------------

11. setCompressed(Boolean) :
If true the data in the body is delivered to the endpoint in the gzip-compressed format. If false, no compression format is used.

SYNTAX :
------------------------------------------------------------
public void setCompressed(Boolean flag)
------------------------------------------------------------

12. getCompressed();
This method will return how the HttpRequest was sent as Compressed format or uncompressed Format.

SYNTAX :
------------------------------------------------------------
public Boolean getCompressed()
------------------------------------------------------------

EXAMPLE :
------------------------------------------------------------
HttpRequest request = new HttpRequest();
request.setCompressed(true);
Boolean flag = request.getCompressed();
System.debug(flag);
------------------------------------------------------------

13. setHeader(key, value) :
This method is used to set the header content of the HttpRequest what we are sending

SYNTAX :
------------------------------------------------------------
public void setHeader(String key, String value)
------------------------------------------------------------

14. getHeader(key) :
This method will return the value from HttpRequest what we have sent to the endpoint based on the key-value

SYNTAX :
------------------------------------------------------------
public String getHeader(key)
------------------------------------------------------------

EXAMPLE :
------------------------------------------------------------
HttpRequest request = new Httprequest();
request.setHeader('Name', 'Ajinkya Dhas');
String name = request.getHeader('Name');
System.debug(name); //Ajinkya Dhas
------------------------------------------------------------

15. setTimeOut()
This method is used to set the Time in milliseconds. This will specify how long the request has for the response if the response is not returned within a given time then it throws the error message

3. HttpResponse :
Use the HttpResponse class to handle the HTTP response returned by the Http class.
Use the DOM Classes or JSON Classes to parse XML or JSON content in the body of a response accessed by HttpResponse.

1. getBody() :
This method will return the response body returned in the response 
Body limit : 6MB for Synchronous and 12 MB for Asynchronous 

SYNTAX :
------------------------------------------------------------
public String getBody()
------------------------------------------------------------

2. getBlobAsBlob() :
This method will return the response as Blob 

SYNTAX :
------------------------------------------------------------
public Blob getBlobAsBlob()
------------------------------------------------------------

3. getBodyDocument() :
This method retrieves body returned in the response as DOM document

SYNTAX :
------------------------------------------------------------
DOM.Documentdomdoc = new DOM.Document(xml);
------------------------------------------------------------

4. getHeader(String key) :
This method returns the contents of the response based on the key is given

SYNTAX :
------------------------------------------------------------
public string getHeader(String key)
------------------------------------------------------------

5. getHeaderKeys() : 
This method returns the array of keys in the response Header

SYNTAX :
------------------------------------------------------------
public String [] getHeaderKeys()
------------------------------------------------------------

6. getStatus() :
This method returns the status message returned in the response

SYNTAX :
------------------------------------------------------------
public string getStatus()
------------------------------------------------------------

7. setBody(String) :
This method is used to set the body of the response

SYNTAX :
------------------------------------------------------------
Public void setBody(String)
------------------------------------------------------------

Now, these syntaxes and methods will help us while creating API's.

So in this way, these Http, HttpRequest, HttpResponse class will help us in upcoming episodes to understand how we can communicate from salesforce to another system. I hope you're enjoying learning with the platform. 

In the next EPISODE, we are going to learn about some important http response codes and it's a purpose. Then we are ready to move towards our hands-on episodes 

Also, I would like to say Big Thanks to you guy's as salesforceKid platform just completed 200K+ Salesforce Readers. I hope this Kid can help you in your future journey as well. 
Also along with Salesforce integration, we are also launching LWC(Lightning Web Component) for you 😁so stay tuned.


WOHOOO !! YOU HAVE JUST COMPLETED SALESFORCE INTEGRATION BASICS EPISODE
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you all
Happy Learning ☁️⚡️ (Learn. Help. Share.) 😊

                                                           NEXT >>

salesforcekid
Salesforce Integration Basics Salesforce Integration Basics Reviewed by on Rating: 5

1 comment:

  1. Anonymous2/24/2020

    Woohoo, thanks a good tutorial, actually I was thinking to learn integration because I am thinking to integrate the 360 SMS App with other software as well

    ReplyDelete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.