+ Start a Discussion
Ian Carswell 36Ian Carswell 36 

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:
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,
                                         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);
                googleMapsJson gro = callGoogleApex(geoAddress);
                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;
                        system.debug('Property Location: ' + Prop.Geolocation__Latitude__s );
                    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');
            HttpResponse res = h.send(req);
            JSONParser parser = JSON.createParser(res.getBody());
            while(parser.nextToken() != null)
                    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.
SwethaSwetha (Salesforce Developers) 
HI Ian, Did you receive any error message? If so, can you post it. Thanks


Did you try running it via Developer Console directly? What error does it provide