• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

How to Get Geolocation Through Apex Class in Salesforce

  • August 1, 2017
  • Merfantz Editor
  • Salesforce Developments Tutorial
  • 0

We have given the solution for how to get Geo location value in the record. Geo location field is available now in Salesforce. Geo location field includes latitude and longitude values. The below example is shown whenever you update billing street, city, state, a country in account object, it will automatically update geolocation value in the record.

Prerequisite :-

1) Create Geolocation Field type in an Object,

2) Create Remote Site for Google Map API,

Trigger –

trigger AccountAddressTrigger on Account (after insert,after update) {

Set<Id> li=new Set<Id>();

if(Trigger.isAfter && Trigger.isUpdate){

for(Account acc:Trigger.New){

if(acc.Geo__Latitude__s ==null){

li.add(acc.id);

LocationCallouts.newAccmethod(li);

}

}

}

}
Apex Class :-

public class LocationCallouts {

@future(callout=true)
static public void newAccmethod(set<id> li){

for(Account a : [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: li]){

String address = ”;

if (a.BillingStreet != null)

address += a.BillingStreet +’, ‘;

if (a.BillingCity != null)

address += a.BillingCity +’, ‘;

if (a.BillingState != null)

address += a.BillingState +’ ‘;

if (a.BillingPostalCode != null)

address += a.BillingPostalCode +’, ‘;

if (a.BillingCountry != null)

address += a.BillingCountry;

address = EncodingUtil.urlEncode(address, ‘UTF-8’);

// build callout

Http h = new Http();

HttpRequest req = new HttpRequest();

req.setEndpoint(‘http://maps.googleapis.com/maps/api/geocode/json?address=’+address+’&sensor=false’);

req.setMethod(‘GET’);

req.setTimeout(5000);
try{

// callout

HttpResponse res = h.send(req);

// parse coordinates from response

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

double lat = null;

double lon = null;

while (parser.nextToken() != null) {

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&

(parser.getText() == ‘location’)){

parser.nextToken(); // object start

while (parser.nextToken() != JSONToken.END_OBJECT){

String txt = parser.getText();

parser.nextToken();

if (txt == ‘lat’)

lat = parser.getDoubleValue();

else if (txt == ‘lng’)

lon = parser.getDoubleValue();

}

}

}

// update coordinates if we get back

if (lat != null){

system.debug(lat+’ ‘+lon);

a.Geo__Latitude__s = lat;
system.debug(a.Geo__Latitude__s+’a.Geo__Latitude__s’);

a.Geo__Longitude__s = lon;
system.debug(a.Geo__Longitude__s +’a.Geo__Longitude__s’);
update a;

}

}

catch (Exception e) {

system.debug(e);

}

}

}

}

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

———————————- We hope this will help to you ———————————-

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
Tags: Field Service ManagementFieldax
  • Previous Business Process Built on Standard Salesforce Objects and Create a New Process
  • Next To Modify Task Composer on Activity Tab in Lightening

Leave a Reply

You must be logged in to post a comment.

Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025
Einstein Activity Capture Boosting Productivity & Relationship Intelligence
Einstein Activity Capture: Boosting Productivity & Relationship Intelligence May 22, 2025
Let Einstein Help You Build - Salesforce Flow
Let Einstein Help You Build – Salesforce Flow May 15, 2025

Copyright @2023 Merfantz Technologies, All rights reserved