2

from this thread, i got the following trigger and class, which have been working yesterday but today not anymore, even no changes have been made from my side. Do you have any idea? In the class, i paste a check comment, to update the field description on account to see, if the class itself got fired and it does...anyhow, it seems to get stucked at the try callout

here is the trigger

trigger SetGeolocation on Account (after insert, after update) {
for (Account a : trigger.new){
    if(Trigger.isUpdate){
       if(a.BillingStreet != Trigger.oldMap.get(a.id).BillingStreet ||
          a.BillingCity != Trigger.oldMap.get(a.id).BillingCity ||
          a.BillingPostalCode != Trigger.oldMap.get(a.id).BillingPostalCode){
            LocationCallouts.getLocation(a.id);
            LocationCallouts.getLocation(a.id);
    }}}} 

and here is the class

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(120000);

      a.Description = a.BillingStreet;
      update a;      

    try{
        // callout
        HttpResponse res = h.send(req);
         System.debug(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();
                   }

            }
        }

        // update coordinates if we get back
        if (lat != null){
            a.Location__Latitude__s = lat;
            a.Location__Longitude__s = lon;

            update a;
        }

    } catch (Exception e) {
    }
}

}

6
  • Have you tried adding "setStatus(status)" to your class in a debug statement? Also, have you set Debug Logs up as you, to see if the @future jobs are encountering an error? Likely, a validation rule or profile change may be the cause since you did not update the classes
    – James
    Oct 2, 2015 at 13:19
  • Hi James, thx for your comment, as i am not a apex pro, can you maybe help me setting up these things? for example the log? what do i need to do? Oct 2, 2015 at 13:30
  • You can retain and manage the debug logs for specific users, including yourself, and for classes and triggers. To view saved debug logs, from Setup, enter Debug Logs in the Quick Find box, then select Debug Logs. When you’ve started retaining debug logs, you can view, download, or delete your logs from this page. Also, replace this line "System.debug(res.getBody());" with "System.debug(res.getStatus());" Once this is done (and you've setup debug logs for your user account, you can update an accont to invoke the trigger and then view the "Debug" statment to get the google server response.
    – James
    Oct 2, 2015 at 14:20
  • I've faced the same issue. It was about Google API limıts for me. You can use system.debug to check if this is the case for you too. If it is about api limits you can get a key from google and use it in your org to avoid google api limits.
    – tugce
    Oct 2, 2015 at 14:30
  • what are you going to do if the trigger size is > 50? Only 50 future calls per transaction. Will affect Data Loader operations or any other bulk operations
    – cropredy
    Oct 3, 2015 at 1:20

1 Answer 1

5

If you are sure that the code was working well and there has been no change since, then it is possible you may have run out of the Google API call limits. https://developers.google.com/maps/documentation/geocoding/usage-limits

Also I don't see the key parameter in the request. If I remember correctly it is required. https://developers.google.com/maps/documentation/geocoding/intro

Normally the response should have the details of either of the scenarios. Check the developer console logs to see what the response says.

EDIT

For going to Debug Logs, goto Setup->Monitoring->Debug Logs and add yourself as the monitored user. Then make the Callout happen again and check the Debug Log record created to see what it says.

8
  • thx for that, i have added the key after the address but before the sensor comment plus I changed the Limit to 600 and now it works, no clue, if it was due to the limit, cause before with key and old limit, it doesn´t work Oct 13, 2015 at 13:10
  • ok, new update...in sandbox, it worked twice in about 10 seconds...but when i deployed the class to live, it did not work...have no clue why...do you have? When i check the api used in google, it shows more then 5 success Oct 13, 2015 at 13:20
  • where can i see in the debug logs, if it is due to api limits not running? or where can i see, where exactly the class is stopping? Oct 13, 2015 at 13:21
  • i checked again in sandbox...i can run it as much as i like, it´s working, but not in live version, do i need to add something else here? can it be caused by access to external websites? Oct 13, 2015 at 13:24
  • Hi Ralf. Check if you have added the URL in the "Remote Site settings" under "Security Controls" in Setup.
    – AslamK
    Oct 13, 2015 at 13:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .