Salesforce enables users to design and build their custom made applications and pages. Also, one can integrate it with external apps using Apex REST and SOAP services. I would like to see the current weather of the shipping city of the selected Account just with a simple click. For this, I need an API that gives information on the weather of the required city. For this, I am using a third-party application that offers the API for the same. In this case, I am using https://openweathermap.org/. Let’s explore the key steps to execute Salesforce API Call to external sources.
We provide CRM development services to strengthen customer relationships. Our developers build custom Salesforce CRM solutions to fulfill unique business requirements.
STEP 1
We need to get the API and key (if required) that are required to integrate with our App.
For this, I have signed up with https://openweathermap.org/ to get the API and the key.
STEP 2
Check if API Enable Permission is checked for the current profile. I have logged in as System Administrator.
STEP 3
Register the site in salesforce from where you would be calling API.
STEP 4
Create a Visualforce Page.
The simplest way to create one is by adding ‘apex/your_page_name’ to the salesforce URL.
Example – https://c.ap0.visual.force.com/apex/testweather
As the page does not exist yet, it will display a Visualforce error saying
Page test weather does not exist
Create Page test weather |
And after clicking on Create Page test weather link, it will create a new visual force page.
You will now be able to edit/modify the page simply by coding in the developer’s console on the same page.
If the console is not visible, please make sure that the Development mode of the current User is enabled.
STEP 5
The Visualforce page replaces the code with the code below.
The Visualforce page supports apex for coding. Its markup is mostly like HTML but with additional attributes and the controller is written in apex which mostly works like java.
We can directly set the value of a variable made in the controller to its markup just by writing the variable in {!variable}
<apex:page standardController=“Account” extensions=“testweatherc”>
<apex:pageBlock title=“{!city} Weather”>
<apex:pageBlockSection >
<apex:outputText label=“Temperature” value=“{!temp}”></apex:outputText>
<apex:outputText label=“Humidity” value=“{!humid}”></apex:outputText>
<apex:outputText label=“Pressure” value=“{!pressure}”></apex:outputText>
<apex:outputText label=“Temperature Minimum” value=“{!temp_min}”></apex:outputText>
<apex:outputText label=“Temperature Maximum” value=“{!temp_max}”></apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Below is the test weather class defined that gives a list of all the accounts using SOQL(Salesforce Object Query Language). Then set shipping city from accounts to a variable and then pass the variable in the parameters in the API along with the key and the other required parameters.
public with sharing class testweather {
public string city{get;set;}
public string temp{get;set;}
public string humid{get;set;}
public string temp_min{get;set;}
public string temp_max{get;set;}
public string pressure{get;set;}
public testweatherc(ApexPages.StandardController stdController) {
Account account = (Account)stdController.getRecord();
account=[Select Id, ShippingCity from Account where Id =:account.Id];
string accountCity = account.ShippingCity;
string apiKey = ‘6e5229f55d5fb2a5d12b7975307007b3‘;
string requestEndPoint = ‘https://api.openweathermap.org/data/2.5/weather’;
requestEndPoint += ‘?q=‘ + accountCity;
requestEndPoint += ‘&units=metrics‘;
requestEndPoint += ‘&APPID=‘ + apiKey;
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndPoint(requestEndPoint);
request.setMethod(‘GET‘);
HttpResponse response = http.send(request);
if(response.getStatusCode() == 200)
{
// deserialize json string into primitive data type.
Map<String, Object> results =(Map<String, Object>)JSON.deserializeUntyped(response.getBody());
system.debug(results);
city = String.valueOf(results.get(‘name‘));
Map<String, Object> mainResults=(Map<String, Object>)(results.get(‘main‘));
temp = String.valueOf(mainResults.get(‘temp‘));
pressure = String.valueOf(mainResults.get(‘pressure‘));
humid = String.valueOf(mainResults.get(‘humidity‘));
temp_min = String.valueOf(mainResults.get(‘temp_min‘));
temp_max = String.valueOf(mainResults.get(‘temp_max‘));
}
else
{
ApexPages.message myMsg=new ApexPages.Message(ApexPages.Severity.ERROR,‘There was an error in reading data.‘);
ApexPages.addMessage(myMsg);
}
}
}
STEP 7
Add a button and place it on Accounts Tab so that it can redirect to test weather Visualforce Page.
To show this button on layout , go to the Accounts tab, click on any account and on the top right corner click on Edit Layout.
And from the top panel drag down the button to the layout (probably in custom buttons area), save it.
Now, it will show the custom button on the layout ( Weather ) and by clicking on this button it will redirect to the Visusalforce page as shown below.
Businesses can follow these steps to easily integrate Salesforce API call to external sources via APEX rest and SOAP services. At Oodles, we provide Salesforce development services to streamline customer information flows. Our developers follow a phased and flexible methodology to ensure businesses get the right solution.
Get in touch with our development team for more details!