Search code examples
geolocationsalesforceapexsalesforce-flow

Unable to make simple Apex class invocable - find custom location record based on coordinate input


I've got a simple class containing a SOQL query that finds the nearest custom location record based on the input of 2 coordinates:

public with sharing class NearestLocation {

@InvocableMethod(label='Get Nearest location' description='From given coordinates the nearest location is returned')
public static List<custom__Location__c> getLocation(List<FlowInput> requests)
{

    List<custom__Location__c> locList =
    [SELECT  id, Name
    FROM custom__Location__c  WHERE RecordType.Name = 'Synced' AND 
    DISTANCE(custom__GeoLocation__c, GEOLOCATION(:requests[0].coordlat, :requests[0].coordlng), 'km')<1
    ORDER BY DISTANCE(custom__GeoLocation__c, GEOLOCATION(:requests[0].coordlat, :requests[0].coordlng), 'km')
                     LIMIT 1];

  for(custom__Location__c lc : locList)
  {
      system.debug('~~!~~!~~' + lc.id);
      system.debug('~~!~~!~~' + lc.name);
  }
        return locList;
}

    public class FlowInput 
    {
        @InvocableVariable(required=true)
        public decimal coordlat;
        @InvocableVariable(required=true)
        public decimal coordlng;
 }   }

The above code works as expected when run from Execute Anon:

list <NearestLocation.FlowInput> fi = new list<NearestLocation.FlowInput>();
NearestLocation.FlowInput x1 = new NearestLocation.FlowInput();
x1.coordlat = 53.243213;
x1.coordlng = -1.475886;
fi.add(x1);
NearestLocation.getLocation(fi);

However, I'm trying to get it to be 'invoked' from within a lightning flow, but it fails with a generic 'flow has validation errors'​ message.

lightning flow - apex action 1



execution log - flow has validation errors 2

I'm obviously missing something and was wondering if anyone could offer some guidance/thoughts?


Solution

  • RESOLVED. 

    Nothing inheritently wrong with the code....problems were due to unrelated formula in the lightning flow!  Hmmm...note to self...start with a blank canvas!

    Thanks for the response Ahmed. It is returning a list: public static List return locList;