Executing Salesforce API Call to External Sources

Posted By : sukirti.sharma | January 16, 2020

Salesforce API call

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.


Steps to Execute Salesforce API Call to External Sources 

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.

Salesforce API Call
Salesforce API Call



Check if API Enable Permission is checked for the current profile. I have logged in as System Administrator.

  1. Go to the left menu under the QuickSearch in Salesforce classic dashboard.
  2. Go to Manage Users -> Profiles, and then Edit System Administrator.
  3. Make sure if API Enabled Option in Administrative Permissions Section is checked.



Register the site in salesforce from where you would be calling API.

  1. Search for Remote Site Settings in Quick search.
  2. Click on New Remote Site
  3. Add Remote Site Name(Open Weather app) and Remote Site URL(https://openweathermap.org/) and Save it.


Salesforce API Call
Salesforce API Call


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.

  1. Go to the left menu under the QuickSearch in Salesforce classic dashboard.
  2. Go to Manage Users -> Users, and then Edit your profile.
  3. Check the Development Mode and Save it.



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>




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();



    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());


    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));




    ApexPages.message myMsg=new ApexPages.Message(ApexPages.Severity.ERROR,There was an error in reading data.);








Add a button and place it on Accounts Tab so that it can redirect to test weather Visualforce Page.

  1. Go to Setup, and under quick search, click on Build -> Customize -> Accounts -> Buttons, Links and Actions.
  2. Then click on New button or Link.
  3. Set name & label, set Display Type as Detail Page Button, set behavior and Content Source should be Visualforce Page, select the specific visual force page in the Content field and save it.


Salesforce API Call


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.


Salesforce API Call
Salesforce API Call


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.


Salesforce API Call
Salesforce API Call


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!

About Author


No Comments Yet.

Leave a Comment

Name is required

Comment is required

Please remove URL from text

Comment is required

Sending message..
Connect with us