• Kush Patel 5
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 4
    Questions
  • 6
    Replies
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());
                    }
            }
    }

 
Has anyone tried to use their field variable as the center of the distance? PLEASE GET BACK TO ME ASAP
public class kushgeocode {

    public List<Account> acct {get; set;}
    //public Integer radius {get; set;}
    public Decimal myLatitude {get; set;}
    public Decimal myLongitude{get; set;}
     
   
    public kushgeocode(ApexPages.StandardController controller) {
    
       
     
 
  
     myLatitude =  40.6423077;
     myLongitude = -74.5680403;              
     acct = [SELECT 
                  Name, 
                  Geocodes__Latitude__s, 
                  Geocodes__longitude__s, 
                  BillingStreet, 
                  BillingCity, 
                  BillingState
             FROM 
                  Account
             WHERE DISTANCE(Geocodes__c, GEOLOCATION(:myLatitude,:myLongitude), 'mi') < :1000];
              
                                                                                     /*^^^Change 10 to :xxxxx*/

        
    }    
     
        
           

    }
public class kushgeocode {

    public List<Account> acct {get; set;}
    public Integer radius {get; set;}
    
    
   
    public kushgeocode(ApexPages.StandardController controller) {
    
      
     
     radius = 10;                  
     acct = [SELECT 
                  Name, 
                  Geocodes__Latitude__s, 
                  Geocodes__longitude__s, 
                  BillingStreet, 
                  BillingCity, 
                  BillingState
             FROM 
                  Account
             WHERE DISTANCE(Geocodes__c, GEOLOCATION(40.6423077,-74.5680403), 'mi') < :radius ];
I have a number field called Radius__c in my Account page. I would like to use whatever value the user inputs as my less than distance also known as "radius" in my code. So basically replace the value ten with the value of the field number given by the user in Radius__c. If anyone has a solution could you please let me know ASAP. Thanks.
 
I would like to be able to mark spots on my map using a marker by drawing out information from a geolocation field but everytime I use !Account.Geocode__c it gives me  this error

Error: Unsupported type: common.api.soap.wsdl.Location used in expression: Account.Geocode__c

Can anyone help me
 
<apex:page standardController="Account">

<apex:map width="100%" height="400px" mapType="hybrid" zoomLevel="15"  
        center="{!Account.BillingStreet},{!Account.BillingCity},{!Account.BillingState}">
       <apex:mapMarker position="{latitude:{!Account.Location},longitude:{Geocode__c}}"/>               
</apex:map>


    
</apex:page>
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());
                    }
            }
    }

 
Has anyone tried to use their field variable as the center of the distance? PLEASE GET BACK TO ME ASAP
public class kushgeocode {

    public List<Account> acct {get; set;}
    //public Integer radius {get; set;}
    public Decimal myLatitude {get; set;}
    public Decimal myLongitude{get; set;}
     
   
    public kushgeocode(ApexPages.StandardController controller) {
    
       
     
 
  
     myLatitude =  40.6423077;
     myLongitude = -74.5680403;              
     acct = [SELECT 
                  Name, 
                  Geocodes__Latitude__s, 
                  Geocodes__longitude__s, 
                  BillingStreet, 
                  BillingCity, 
                  BillingState
             FROM 
                  Account
             WHERE DISTANCE(Geocodes__c, GEOLOCATION(:myLatitude,:myLongitude), 'mi') < :1000];
              
                                                                                     /*^^^Change 10 to :xxxxx*/

        
    }    
     
        
           

    }
public class kushgeocode {

    public List<Account> acct {get; set;}
    public Integer radius {get; set;}
    
    
   
    public kushgeocode(ApexPages.StandardController controller) {
    
      
     
     radius = 10;                  
     acct = [SELECT 
                  Name, 
                  Geocodes__Latitude__s, 
                  Geocodes__longitude__s, 
                  BillingStreet, 
                  BillingCity, 
                  BillingState
             FROM 
                  Account
             WHERE DISTANCE(Geocodes__c, GEOLOCATION(40.6423077,-74.5680403), 'mi') < :radius ];
I have a number field called Radius__c in my Account page. I would like to use whatever value the user inputs as my less than distance also known as "radius" in my code. So basically replace the value ten with the value of the field number given by the user in Radius__c. If anyone has a solution could you please let me know ASAP. Thanks.
 
Hey guys, I have a trigger with code:
/*// Trigger runs getLocation() on Accounts with no Geolocation

trigger SetGeolocation on Account (after insert, after update) {
    for (Account a : trigger.new)
    if (a.Location__Latitude__s == null)
          LocationCallouts.getLocation(a.id);
}*/

// Trigger runs getLocation() on Accounts with no Geolocation

trigger SetGeolocation on Account (after insert, after update) {
    for (Account a : trigger.new){
        if(system.isFuture() == FALSE){
          LocationCallouts.getLocation(a.id);
        }
    }
}

This pulls an apex class that has this code:
 
public class LocationCallouts {

     @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id accountId){
        // gather account info
        Account a = [SELECT BillingCity,BillingCountry,BillingPostalCode,BillingState,BillingStreet FROM Account WHERE id =: accountId];

        // create an address string
        String address = '';
        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');

        // build callout
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://maps.googleapis.com/maps/api/geocode/json?address='+address+'&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){
                a.Location__Latitude__s = lat;
                a.Location__Longitude__s = lon;
                update a;
            }

        } catch (Exception e) {
        }
    }
}

The problem is, I am going above the 10 max limit of future calls in Apex...

can someone please explain how I would convert my apex class intoa batch apex class so it runs request one at a time? See this threaD:

https://developer.salesforce.com/forums/ForumsMain?id=906F000000091NoIAI