function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kush Patel 5Kush Patel 5 

Batch Apex help

If anyone could answer within the next couple of days, it would be greatly appreciated.
I'm running into a problem when I try to import data using the data loader. I want to import about 800 account records but everytime I try the records fail.
Here is my batch apex
global class LocationCallouts_2  implements Database.Batchable<sObject>, Database.AllowsCallouts{
        
    Id accountId; 
    Account[] a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];
    
    global Iterable<sObject> start(Database.BatchableContext BC){
        return (a);
    }
    
    global void execute(Database.BatchableContext BC, List<sObject> scope){
        Account a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];
        String geocodeKey = 'AIzaSyBo-6WwACjhUdg-I81NDtZK87r2xpv8b0U';
        String address = '';
        
        for (sObject s : scope){     
             
            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+ '&key='+geocodeKey+'&sensor=false');
            req.setMethod('GET');
            req.setTimeout(60000);
            
            try{
                // callout
                HttpResponse res = h.send(req);
                
                // parse coordinates from response
                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();
                            }
                        }
                }
                
                // update coordinates if we get back
                if (lat != null){
                    lat = a.Geocodes__Latitude__s;
                    lon = a.Geocodes__Longitude__s;
                    update a;
                }
                
            } 
            catch (Exception e){
            }
            
        }
	//LocationCallouts_2 batch = new LocationCallouts_2();
	//Database.executeBatch(new LocationCallouts_2(),20);
    }
      @future (callout=true)
    public static void startbatch(){
        LocationCallouts_2 batch = new LocationCallouts_2();
        Id batchprocessId = database.executeBatch(batch);
        System.debug('Apex Job Id' + batchprocessId);
    }
    
    global void finish(Database.BatchableContext BC){
        
    }
    
    
}

And here is my trigger
// Trigger runs getLocation() on Accounts with no Geolocation
/*
trigger SetGeolocation on Account (after insert, after update) 
    {
        for (Account a : trigger.new)
            {
                Boolean addressChangedFlag = false;
                
                if (a.Geocodes__Latitude__s == null)
                    {
                        LocationCallouts.getLocation(a.id);
                    }
            }
    }
*/

trigger SetGeolocation on Account (after insert, after update) 
    {
        for (Account a : trigger.new)
            {
                Boolean addressChangedFlag = false;
                
                if (a.Geocodes__Latitude__s == null)
                    {
                        Database.executeBatch(new LocationCallouts_2());
                    }
            }
    }

 
JeffreyStevensJeffreyStevens
I don't think you want to call the batch class for every record that fired the trigger.  Remember - a trigger can get fired with up-to 200 records at a time.

I assume you would want to loop through your triggered records, collecting the ID's of the null Geocodes, and then pass those IDs to the Batchable class.  
Kush Patel 5Kush Patel 5
Yes that's what I want to do. So you are saying I can get rid of my trigger but how is my class going to run?
K_TK_T
What exactly are you trying to do with the accounts? It seems like you're updating the address fields? Another way you could run the class is using execute anonymous. You'd have to give it a query string to use in the start method to query the records from the DB that you want to update. 
Kush Patel 5Kush Patel 5
I want to add accounts from an excel sheet. I want to update my gelocation fields using a Google web service. I have already done that but it only updates 50 at a time.
Kush Patel 5Kush Patel 5
Anybody know what I'm talking about?