Show record city Temperature on record detail page in Salesforce.

Posted on September 23, 2021 Post Thumbnail

Yo Trailblazers,

In this blog post, I'm going to create a lightning component which shows temperature of record city on record detail page.

Check following image for demo.


What you'll Learn from Blog Post :)

1. How to make callout to rest api from Apex.

2. Parse returned response using Apex.

Walkthrough :)

1. First we need to get Api Key from

2. Add to Remote sites in Salesforce.

3. Than, we require current record id.

4. With the help of record id, query city.

5. At the end, We need to make callout and show temperature on record page.

Let's Start :)

I'm going to use account object for this task.

1. First we need to get Api Key from

For this you need to sign up, 

If you don't want to sign up, Use my API Key:

String Api key = '5236dbbf932439eef162fb321536f5e7';

2. Add to Remote sites in Salesforce.

Setup > Remote Site Settings


Now, Let's create Apex Class.

public class AccountWeatherClass {
    public static list<String> getTemprature(String AccountId) {
        String city;
        String endpoint = '';
        String apikey = '5236dbbf932439eef162fb321536f5e7';
        city = [SELECT Id, billingcity FROM Account Where Id =:AccountId].billingcity;
        String mainUrl = endpoint + city + '&appid=' + apikey + '&units=metric';
        list<String> resList = new list<String>();
        String temp;
        String temp_max;
        String temp_min;
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = h.send(req);
        System.debug('response:--> ' + res.getBody());
        JSONParser parser =  JSON.createParser(res.getBody());
        while (parser.nextToken() != null) {
            if(parser.getText() == 'temp') 
                System.debug('temp'+ parser.getText());
                system.debug('Main temp_max-->' + parser.nextValue());
                temp = parser.getText();
            if(parser.getText() == 'temp_max') 
                System.debug('temp_max'+ parser.getText());
                system.debug('Main temp_max-->' + parser.nextValue());
                temp_max = parser.getText();
            if(parser.getText() == 'temp_min') 
                System.debug('temp_min'+ parser.getText());
                system.debug('Main temp_min-->' + parser.nextValue());
                temp_min = parser.getText();
        String fieldName = parser.getCurrentName();
        return resList;

In above getTemprature method, I'm passing account Id as parameter and on the base of Account Id Querying City.

And with the help of city and api key, Making callout.

Getting following response:


 After getting a response, I'm creating parser using

JSONParser parser = JSON.createParser(res.getBody());

And filtering required values such as temp, temp_min, temp_max and returning these values.

Now, Let's create Aura component


<aura:component controller="AccountWeatherClass" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    <aura:attribute name="temp" type="String"/>
    <aura:attribute name="temp_min" type="String"/>
    <aura:attribute name="temp_max" type="String"/>
    <lightning:card iconName="standard:account" title="Real time Temprature">
        <p class="slds-p-horizontal_small">
            <h1>Account City Temp : {!v.temp} Celsious</h1> 
            <h1> Account City Max Temp : {!v.temp_max} Celsious</h1> 
            <h1> Account City Min Temp : {!v.temp_min} Celsious</h1>


	doInit : function(component, event, helper) {
		var accId = component.get("v.recordId");
        var action = component.get("c.getTemprature");
        action.setCallback(this, function(response) {
            var res = response.getReturnValue();

After creating above component add it to account record page.


If you have any question Ask Me

Thanks for Reading :)

Write a comment for suggestions and hit the heart icon.


Tags: #lightningComponent #httpCallout


There are no comments yet.
Your message is required.

Get notified of new posts