When i run my batch apex latitude and longitude are not getting updated in customer object. i need to update 50000 record.. getting lat and long using google API i able to request 2500 record per IP. even though 2500 record not get updated. but when i ping Endpoint URL manually getting JSON response containing lat and long. kindly any one help me out on this issue. "http://maps.googleapis.com/maps/api/geocode/json?address=Slupsk+poland+76-251+&sensor=false%27" Here is my Batch apex code:
global class batchAccountGeofieldUpdate implements Database.Batchable < sObject > , Database.AllowsCallouts, Database.Stateful {
global Database.QueryLocator start(Database.BatchableContext BC) {
//query on Task
String query = 'SELECT id,BillingStreet,BillingAddress,BillingCity,BillingCountry,BillingPostalCode,BillingState,geoLocation__Longitude__s,geoLocation__Latitude__s FROM Account where geoLocation__Latitude__s = null and geoLocation__Latitude__s';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List < Account > Accountupdate) {
List < Account > modifiedAccounts = new list < Account > ();
for (Account a: Accountupdate) {
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();
if (address != null)
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);
// parse coordinates from response
system.debug('Response' + Res);
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();
system.debug('++++lat +++++' + lat);
} else if (txt == 'lng') {
lon = parser.getDoubleValue();
system.debug('++++lon+++++' + lon);
}
}
}
}
// update coordinates if we get back
if (lat != null) {
a.geoLocation__Latitude__s = lat;
a.geoLocation__Longitude__s = lon;
modifiedAccounts.add(a);
system.debug('++++a.BillingLatitude+++++' + a.geoLocation__Latitude__s + '====' + a.geoLocation__Longitude__s);
}
} catch (Exception e) {}
}
update modifiedAccounts;
}
global void finish(Database.BatchableContext BC) { }
}
Code to execute:
batchAccountGeofieldUpdate Accountbatch = new batchAccountGeofieldUpdate();
Database.executeBatch(Accountbatch,50);