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
Shane K 8Shane K 8 

How to return values from this class

Hello,

How can I return lat and lon from the class? I am passing Address to the getLatitude_Longitude method and would like to return the lat and lon.
 
public class GetMapLocation {
     @future (callout=true)
    static public void getLatitude_Longitude(String Address){
        
        Address = EncodingUtil.urlEncode(Address, 'UTF-8');
        
        // build callout
        Http h = new Http();        
        HttpRequest req = new HttpRequest();
        
	    String yourAPiKey = 'MyKey'; // Enter your Api key which you have created in google api console
        String key = '&key=' + yourAPiKey;
        String endPoint = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + Address;
		
        req.setEndpoint(endPoint + key);
        req.setMethod('GET');
        req.setTimeout(5000);
        System.debug('Request ' + req);
        try{
            // callout
            HttpResponse res = h.send(req);
            // parse coordinates from response
            JSONParser parser = JSON.createParser(res.getBody());
            system.debug('Response get Body ' + res.getBody());
            
            double lat = null;
            double lon = null;
            //system.debug(parser.nextToken());
            while (parser.nextToken() != null) {  
                system.debug(parser.getCurrentToken());
                system.debug(JSONToken.FIELD_NAME);
                if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'location')){
                       // system.debug('a');
                        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
            system.debug(lat);
            if (lat != null){
                system.debug(lat+' '+lon);              
            }
        }
        catch (Exception e) {
            system.debug(e);
        }
    }
}

 
Best Answer chosen by Shane K 8
David Zhu 🔥David Zhu 🔥
The simpliest solution I would suggest is to use string.

static public String getLatitude_Longitude(String Address){
string result = '';
.....

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'location')){
                       // system.debug('a');
                        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();
                            
                             result = string.valueof(lat) + ',' + string.valueOf(lont);
                        }
                    }

    return result;
}

Or you can create an inner class:

public class LatLonClass 
{
           public double Lat {get;set;}
           public double Lon {get;set;}
}


static public LatLonClass getLatitude_Longitude(String Address){
LatLonClass result = new LatLonClass ();

.....

                              if (txt == 'lat')
                                lat = parser.getDoubleValue();
                            else if (txt == 'lng')
                                lon = parser.getDoubleValue();
                            
                             result.lat = lat;
                             result.lon = lon;
       return result;
}

 

All Answers

David Zhu 🔥David Zhu 🔥
The simpliest solution I would suggest is to use string.

static public String getLatitude_Longitude(String Address){
string result = '';
.....

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
                    (parser.getText() == 'location')){
                       // system.debug('a');
                        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();
                            
                             result = string.valueof(lat) + ',' + string.valueOf(lont);
                        }
                    }

    return result;
}

Or you can create an inner class:

public class LatLonClass 
{
           public double Lat {get;set;}
           public double Lon {get;set;}
}


static public LatLonClass getLatitude_Longitude(String Address){
LatLonClass result = new LatLonClass ();

.....

                              if (txt == 'lat')
                                lat = parser.getDoubleValue();
                            else if (txt == 'lng')
                                lon = parser.getDoubleValue();
                            
                             result.lat = lat;
                             result.lon = lon;
       return result;
}

 
This was selected as the best answer
Shane K 8Shane K 8
Hi David,

Thank you.

I am getting this following error: Future methods do not support return type of String

I have a @future (Callout=true) as I am calling this class from a trigger.
David Zhu 🔥David Zhu 🔥
If trigger is the only place to call the method, it cannot have a return type other than void. I missed that part.
The reason is Callout method is running in a different thread. It is in fire and forget mode. So trigger does not wait the callout completion.

In this case, we usually write the lat and lon value to a Salesforce object through DML.