1

I tried this code getting latitude and longitude from google map. But the field is not updating.


public class LocationCallouts {

     @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id accountId){
      Account a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];
         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');
        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(60000);

        try{

            HttpResponse res = h.send(req);
            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();
                       }
                }
            }
            if (lat != null){
                a.Location__Latitude__s = lat;
                a.Location__Longitude__s = lon;
                update a;
                            }
                } catch (Exception e) {
        }   } }

> 

trigger SetGeolocation on Account (after insert, after update) {
    for (Account a : trigger.new)
        if (a.Location__Latitude__s == null)
            LocationCallouts.getLocation(a.id);
}
  • Whats the issue? Did you added some debug statement to check whether you are getting correct response from google or not? – Santanu Halder Dec 11 '15 at 9:34
0

Please check the below:

  1. Whether you have the remote site settings configure for "http://maps.googleapis.com" url.

  2. You need to query Location__Latitude__s and Location__Longitude__s field of Account before updating them.

| improve this answer | |
  • i remove try catch block from code now its working if i use try,catch means location fileld updating empty even i enter address also......... – user123 Dec 11 '15 at 9:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.