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
ericmonteericmonte 

Geolocation Trigger bulkyfication HELP, too many future calls

Hey all, so awhile back i asked helped on the trigger Geolocation functionality to google. I found the information i need to create a trigger to google from

http://blog.internetcreations.com/2012/09/creating-a-geolocation-trigger-in-salesforce-winter-13/

 

and everything works just fine now. But I am having a hard time bulkifying this code. Can someone help me?

 

trigger trgGeoCode on Address__c (after insert, after update) {
    
    List<Address__c> addList = [select id, AddressGeoLocation__Latitude__s, AddressGeoLocation__Longitude__s 
    from Address__c where Id IN: Trigger.newMap.keySet()
    and Account__r.Type__c includes (: 'Facility') ];
    
    for (Address__c a : addList){
        
        if (a.AddressGeoLocation__Latitude__s ==null ){
            ClassGoogleGeoLocationCallout.getLocation(a.id);
        }
    }
}

 and my main class stays the same based off the blog:

public class ClassGoogleGeoLocationCallout {

@future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id addId){
        // gather account info
        Address__c a = [select Name, City__c, State__c,Zip__c, Latitude__c, Longitude__c, Country__c
        from Address__c 
        where id =: addId]; 
         
        // create an address string
        String address = '';
        if (a.Name != null)
            address += a.Name +', ';
        if (a.City__c != null)
            address += a.City__c +', ';
        if (a.State__c != null)
            address += a.State__c +' ';
        if (a.Zip__c != null)
            address += a.Zip___c +', ';
        if (a.Country__c != null)
        address += 'USA';
 
        address = EncodingUtil.urlEncode(address, 'UTF-8');
 		system.debug('=== ADDRESS ===' +address);
        // 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);
 			System.debug('====RESPONSE====' +res.getBody());
            // 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();
                       }
                       System.debug('====LAT====' +lat);
 
                }
            }
 
            // update coordinates if we get back
            if (lat != null){
                a.AddressGeoLocation__latitude__s = lat;
                a.AddressGeoLocation__longitude__s = lon;
                update a;
            }
 
        } catch (Exception e) {
        }
    }

 So in the end when I try to bulk update using dataloader i get an error saying:

System.LimitException: Too many future calls: 11.

 

Thanks for the help

Best Answer chosen by Admin (Salesforce Developers) 
ericmonteericmonte

Due to my lack of knowledge in SFDC Web Service call out, my code will never be bulkified due to SFDC governor limits. As a result this trigger should not even be used for BULK Insert or Update, rather use a Batch Apex scheduler.

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_apex_governor_limits.htm

All Answers

ericmonteericmonte

Due to my lack of knowledge in SFDC Web Service call out, my code will never be bulkified due to SFDC governor limits. As a result this trigger should not even be used for BULK Insert or Update, rather use a Batch Apex scheduler.

 

http://www.salesforce.com/us/developer/docs/pages/Content/pages_apex_governor_limits.htm

This was selected as the best answer
Shravani M 9Shravani M 9
Hi,

I am facing the same issue. Can you explain me in step by step how to achieve the task.