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
Milan HrdlickaMilan Hrdlicka 

non-functional trigger using an instance of a class making webservice callout

Hi,

I have a problem with a non-functioning trigger.
I have custom object Branch__c showing fields like street, street number, city, postal code, etc.
Field Concatenate_Address__c is a formula field which concatenates all adress details and makes up a String used as an argument for getGPS() function which makes a web service call.
Example of the string (Concatenate_Address__c): 'http://maps.googleapis.com/maps/api/geocode/json?address=Černokostelecká /, Říčany, 251 01'
In my trigger I used gps.setEndPointStr('&addrString&'); to construct a supplying argument for the getGPS() function in order to return latitude and longitude details as per trigger and mentioned class code below.
The desired outcome is a latitude and longitude details update for each row once address details are entered.
It does not work, I tried to supply "concrete" details (like Example of the string - commented out in the trigger) but it did not work anyway, I guess there must be a problem using the gps.getLat(); and gps.getLng(); functions in the trigger.
Can you please advise how to amend the trigger in order to get desired GPS details updated?

Really appreciate your help.
Milan


my non-functioning trigger
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
trigger updGPStest on Branch__c (before insert, before update) {
     
    List<Branch__c> addrString = new List<Branch__c>();
    getGPS gps = new getGPS();
    
    for(Branch__c br1 : Trigger.New) {
    

        addrString = [SELECT Concatenate_Address__c FROM Branch__c WHERE Id IN :Trigger.New];
        
        gps.setEndPointStr('&addrString&');
        
        br1.Location_GPS__latitude__s = gps.getLat();
        br1.Location_GPS__longitude__s = gps.getLng();
        
        
        
        //gps.setEndPointStr('http://maps.googleapis.com/maps/api/geocode/json?address=Černokostelecká /, Říčany, 251 01');
        //br1.Test_Column__c = br1.Concatenate_Address__c;
        

    }
}


class getGPS:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class getGPS {
    
    //String endPointString = 'http://maps.googleapis.com/maps/api/geocode/json?address=Černokostelecká /, Říčany, 251 01';
    String endPointString;
    double lng;
    double lat;
    
    public void setEndPointStr(String input) {
        
       this.endPointString = input;
        
    }    
    
    
    public double getLng() {
        return lng;
        }
    
    public double getLat() {
        return lat;
        }
    
  

      public void getGPSdata() {
          Http httpProtocol = new Http();
          HttpRequest request = new HttpRequest();
          String endpoint = endPointString;
          request.setEndPoint(endpoint);
          request.setMethod('GET');
          HttpResponse response = httpProtocol.send(request); 
          String jsonString = response.getBody();
          googleAddress addr = googleAddress.parse(jsonString);
          
googleAddress.Location loc = addr.firstLoc;
double lng = loc.lng;
double lat = loc.lat;
          
          this.lng=lng;
          this.lat=lat;

      }
    

    

                       
}

class googleAddress
​-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public class googleAddress {

    public class Address_components {
        public String long_name;
        public String short_name;
        public List<String> types;
    }

    public class Geometry {
        public Location location;
        public String location_type;
        public Viewport viewport;
    }

    public List<Results> results;
    public String status;
    public Location firstLoc;

    public class Results {
        public List<Address_components> address_components;
        public String formatted_address;
        public Geometry geometry;
        public Boolean partial_match;
        public String place_id;
        public List<String> types;
    }

    public class Viewport {
        public Location northeast;
        public Location southwest;
    }

    public class Location {
        public Double lat;
        public Double lng;
    }

    
    public static googleAddress parse(String json) {
        
        googleAddress returnAddr;
        
        returnAddr = (googleAddress) System.JSON.deserialize(json, googleAddress.class);
        if (!returnAddr.results.isEmpty()){
            returnAddr.firstLoc = returnAddr.results[0].geometry.location;
        }
        return returnAddr; 
        
        
    }
}

​-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Akhil ReddyAkhil Reddy
I don't  getGPSdata() method was called anywhere in your trigger so your trigger would not return anything.
Milan HrdlickaMilan Hrdlicka
getGPS() instance is called before the "for" cycle and if you put in in for cycle the result is still the same.