You need to sign in to do that
Don't have an account?

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
And here is my trigger
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()); } } }
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.