Apex Callouts to Display External Data in Salesforce

Salesforce provides a way to fetch external data using Apex callouts. The Apex code uses the Remote Site Settings URL to retrieve the data from an endpoint, and also the Apex integrates with Web services by using SOAP, WSDL, and HTTP (REST).

Salesforce provides a way to fetch external data using Salesforce Apex callout. The Apex code uses the Remote Site Settings URL to retrieve the data from an endpoint, and also the Apex integrates with Web services by using SOAP, WSDL, and HTTP (REST).

The example below shows how to display the external data in Salesforce from an endpoint using Apex callouts.

Step 1: Create a Remote Site Settings.

URL (https://raw.githubusercontent.com)

The URL contains the candidate details in JSON format with a few fields: Candidate name, Account number, Source, Website, and Email.

Step 2: Create a Wrapper Class

Consolewrap.apxc

public class consolewrap {

public String Name{get;set;}

public String AccNumber{get;set;}

public String Source{get;set;}

public String Site{get;set;}

public String Email{get;set;}

}

The Consolewrap wrapper class contains variables to hold the values from the external data source.

Step 3: Create a Visualforce page

Callout.vfp

<apex:page controller="calloutcontroller" title="JSON table" >

<apex:form >

<apex:pageBlock >

<apex:pageBlockTable value="{!performcallout}" var="wrap" width="100%">

<apex:column headerValue="Name" value="{!wrap.Name}"/>

<apex:column headerValue="AccountNumber" value="{!wrap.AccNumber}"/>

<apex:column headerValue="Source" value="{!wrap.Source}"/>

<apex:column headerValue="Website" value="{!wrap.Site}"/>

<apex:column headerValue="Email" value="{!wrap.Email}"/>

</apex:pageBlockTable>

</apex:pageBlock>

</apex:form>

</apex:page>

The VF page contains the PageBlockTable to display the candidate details in a table format by calling the Controller class.

Step 4: Create an Apex Class

Calloutcontroller.apxc

public class Calloutcontroller {

public List<consolewrap> ConsoleWrapperList{get;set;}

public List<consolewrap> getperformcallout(){

ConsoleWrapperList = new List<consolewrap>();

HttpRequest req = new HttpRequest();

HttpResponse res = new HttpResponse();

Http http = new Http();

req.setEndpoint
('https://raw.githubusercontent.com/
parthiban019/samplejson/master/
student.json');

req.setMethod('GET');

res = http.send(req);

if(res.getstatusCode() == 200 && res.getbody() != null){

ConsoleWrapperList=(List<consolewrap>)
json.deserialize(res.getbody(),
List<consolewrap>.class);

}

return consolewrapperlist;

}

}

The Calloutcontroller class uses the HTTP request and GET method to retrieve the data from the endpoint which is in JSON format and the class populates the external data in a wrapper list used to display the data on the Visualforce page.

Step 5: Execute the Visualforce page

Salesforce Apex Callout

Now, the Visualforce page displays the data from the external system.

Reference Link: Salesforce: Apex Web Services and Callouts

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Harnessing Generative AI in Healthcare Insurance: Streamlining Operations and Enhancing Member Insights

Healthcare insurance providers are navigating new ground and capitalizing on new opportunities made possible through artificial intelligence. AI can be utilized to lower costs for payers and improve the member experience as well as the overall health and well-being of members, leading to greater member satisfaction and improving trust and reputation for the payer’s brand.

Read Article »

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.