You need to sign in to do that
Don't have an account?
Need help with Batch Apex.
I am a long time Salesforce admin/developer but I am still getting started with actually writing Apex code so this is probably a simple error on my part.
I am trying to use batch apex to call the google maps api to avoid limits on @future calls. We have a custom Property__c object and we need to be able load hundreds of records at a time and have them get geocoded.
I can tell from the debug statements that I am successfully passing a set of Ids to the batch apex class. However the database.querlocator does not appear to be getting called. Here is the code:
I am trying to use batch apex to call the google maps api to avoid limits on @future calls. We have a custom Property__c object and we need to be able load hundreds of records at a time and have them get geocoded.
I can tell from the debug statements that I am successfully passing a set of Ids to the batch apex class. However the database.querlocator does not appear to be getting called. Here is the code:
public class batchPropertyGeocode implements Database.Batchable <sObject>, Database.AllowsCallouts, Database.Stateful { Set<Id> propsToGeocode = new Set<Id>(); //Constructor will take a set of Property Ids public batchPropertyGeocode(Set<Id> propsToGeocode) { this.propsToGeocode = propsToGeocode; System.debug('Creating list of Properties to Geocode: '+this.propsToGeocode); } public Database.QueryLocator start(Database.BatchableContext BC) { //Query Properties passed from Trigger System.debug('Querying to get Property addresses'); return Database.getQueryLocator([SELECT Property_City__c,PropertyState__c, Property_Street_Address__c,PropertyZipCode__c FROM Property__c WHERE Id IN :propsToGeocode]); } public void execute(Database.BatchableContext BC, List <Property__c> Propertyupdate) { List < Property__c > modifiedProps = new list <Property__c> (); for (Property__c prop: Propertyupdate) { //create a string for the address to pass to Google Geocoding API String geoAddress = ''; if(prop.Property_Street_Address__c!= null) geoAddress+= prop.Property_Street_Address__c+ ', '; if(prop.Property_City__c != null) geoAddress+= prop.Property_City__c+ ', '; if(prop.PropertyState__c!= null) geoAddress+= prop.PropertyState__c+ ', '; if(prop.PropertyZipCode__c!= null) geoAddress+= prop.PropertyZipCode__c; geoAddress = EncodingUtil.urlEncode(geoAddress, 'UTF-8'); System.debug('Url Encoded address: '+ geoAddress); try{ googleMapsJson gro = callGoogleApex(geoAddress); System.Debug(gro.results.size()); if(gro.results.size() > 0) { double lat = gro.results[0].geometry.location.lat; double lon = gro.results[0].geometry.location.lng; if (lat != null) { system.debug('lat is not null'); prop.Geolocation__Latitude__s = lat; prop.Geolocation__Latitude__s = lon; modifiedProps.add(prop); system.debug('Property Location: ' + Prop.Geolocation__Latitude__s ); } } else { System.Debug('nothing in list. what am i going to do?'); } } catch (Exception e) {} update modifiedProps; } } public static googleMapsJson callGoogleApex(string geoAddress) { // Key for Google Maps Geocoding API String geocodingKey = 'Actual Google API Key Here'; Http h = new Http(); HttpRequest req = new HttpRequest(); googleMapsJson gro = new googleMapsJson(); if (geoAddress != null) { req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address=' + geoAddress +'&key='+ geocodingKey+ '&sensor=false'); req.setMethod('GET'); req.setTimeout(6000); HttpResponse res = h.send(req); JSONParser parser = JSON.createParser(res.getBody()); while(parser.nextToken() != null) { if(parser.getCurrentToken()==JSONToken.START_OBJECT){ gro = (googleMapsJson) parser.readValueAs(googleMapsJson.class); } } } return gro; } public void finish(Database.BatchableContext BC) { } }I would really appreciate some help to get me over the hump.
Ian,
Did you try running it via Developer Console directly? What error does it provide
Thanks,
Gaurav