• Ian Carswell 36
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
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,
                                         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.
 
I recently started a new job and I have been trying to get the Data Loader CLI working. I have used it many times before.

I have generated a key and encrypted password several times. However I still get an error when I try to login:

 [LoginFault [ApiFault  exceptionCode='INVALID_LOGIN'
 exceptionMessage='Invalid username, password, security token; or user locked out.'
 extendedErrorDetails='{[0]}']

I know that I am hitting the correct endpoint because I can see the attempt in the login history:

Login Type    Status            Browser            Platform Application
Other Apex API    Invalid Password    Java (Salesforce.com)    Unknown    DataLoaderPartnerBatch/

I have tried adding my security token to my password but still no joy.

So it looks like something is going wrong with encrypting/decrypting my password.


Any ideas?