0

Following my previous question, I have 2 fields :

1.) Location

2.) Dest_Location

I followed this link to make the apex class and trigger. But, when I put two triggers to get me geo location, it does not work. However, if I write only one of them, it works like it should.

So, what I think basically is that if I could implement both of the apex classes in one trigger?

My code for Apex Class for Location, along with Trigger code is HERE

My code for Apex Class for Dest_Location, along with Trigger code is HERE

I've added the code here as well, please scroll down :)

How can I merge these into one trigger?

FOR Location

// APEX CLASS IS HERE !

public class geolucationtry {




     @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) {
        }
    }




}




// THIS IS MY TRIGGER CODE :

trigger location_set on Account (after insert, after update) 
{
    for (Account a : trigger.new)
        if (a.Location__Latitude__s == null)
            geolucationtry.getLocation(a.id);



}

FOR Dest_Location

// APEX CODE IS HERE!!!

public class dest_geolucationtry
{

         @future (callout=true)  // future method needed to run callouts from Triggers
      static public void getLocation(id accountId){
        // gather account info
        Account b = [SELECT ShippingCity,ShippingCountry,ShippingPostalCode,ShippingState,ShippingStreet FROM Account WHERE id =: accountId];

        // create an address string
        String address = '';
        if (b.ShippingStreet != null)
            address += b.ShippingStreet +', ';
        if (b.ShippingCity != null)
            address += b.ShippingCity +', ';
        if (b.ShippingState != null)
            address += b.ShippingState +' ';
        if (b.ShippingPostalCode != null)
            address += b.ShippingPostalCode +', ';
        if (b.ShippingCountry != null)
            address += b.ShippingCountry;

        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 lati = null;
            double longi = 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')
                               lati = parser.getDoubleValue();
                           else if (txt == 'lng')
                               longi = parser.getDoubleValue();
                       }

                }
            }

            // update coordinates if we get back
            if (lati != null){
                b.Dest_Location__Latitude__s = lati;
                b.Dest_Location__Longitude__s = longi;
                update b;
                system.debug(b);
            }

        } catch (Exception e) {
        }
    }



}




// THIS IS TRIGGER CODE :

trigger location_set on Account (after insert, after update) 
{

    for (Account b : trigger.new)
        if (b.Dest_Location__Latitude__s == null)
            dest_geolucationtry.getLocation(b.id);


}
4
  • Please put your code here, so that it easy to get the problem. Commented Jul 23, 2016 at 15:32
  • the post will be TOO long to read if I do that. That's why I posted on github gist.
    – Anony-Kun
    Commented Jul 23, 2016 at 15:33
  • you want to use logical and operator or merge multiple triggers into one. Commented Jul 23, 2016 at 15:34
  • that is the question... how do I merge two triggers into one? I tried certain things, which didn't work. Perhaps a simple example could help...
    – Anony-Kun
    Commented Jul 23, 2016 at 15:36

1 Answer 1

4

I think you can use the below code:

    trigger location_set on Account (after insert, after update) 
{

    for (Account a : trigger.new)
    {
        if (a.Dest_Location__Latitude__s == null)
            dest_geolucationtry.getLocation(a.id);
        if (a.Location__Latitude__s == null)
            geolucationtry.getLocation(a.id);
    }
}
5
  • It worked, thank you. I was making 2 for loops, I guess that's where I was wrong. Thank you
    – Anony-Kun
    Commented Jul 23, 2016 at 15:51
  • Glad it works for you :) Commented Jul 23, 2016 at 15:53
  • it was working a while ago, but I'm getting Future method cannot be called from a future or batch method.
    – Anony-Kun
    Commented Jul 24, 2016 at 3:30
  • It is because you called both classes and in both class you have future methods. It will create problem. I think you need to use Batch apex for this. There are multiple thread related to your problem. I wish please visit them also. Commented Jul 24, 2016 at 9:52
  • Great, you solved it (y) Commented Jul 24, 2016 at 10:00

You must log in to answer this question.

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