无法调用简单Apex类:基于坐标查找自定义Location记录遇问题
I’ve run into similar Flow + Invocable Apex headaches before—those generic "validation errors" messages are so frustrating because they don’t tell you the real root cause. Let’s break down the most likely issues and fixes based on your code and scenario:
1. Mismatched Return Value Handling in Flow
Your Apex method returns a List<custom__Location__c>, but if you’re trying to store this output into a single record variable in Flow instead of a collection variable, that’ll trigger a validation error right away.
Fix:
- When configuring the Apex action’s "Store Output Values" section in your Flow, make sure the target variable is a collection of
custom__Location__crecords (not a single record). If you don’t have one set up, create a new variable with type "Record" and check the "Allow multiple values (collection)" box.
2. Record Type Query Risks
Your SOQL uses RecordType.Name = 'Synced'—this is unreliable because Record Type Labels can be changed or localized, and the user running the Flow might not have access to that Record Type. Also, if the Developer Name differs from the Label (e.g., Synced_Location instead of Synced), this query won’t return any results, leading to unexpected Flow behavior.
Fix:
- Switch to using the Record Type’s Developer Name (it’s far more stable):
RecordType.DeveloperName = 'Synced' - Verify that the user running the Flow has permission to view records of the "Synced" Record Type for
custom__Location__c.
3. Geolocation Field Permissions & Data Issues
If the user running the Flow doesn’t have read access to the custom__GeoLocation__c field, the SOQL will fail silently (Flow just shows a generic error). Also, if none of your Location records have valid coordinates in this field, the DISTANCE function might behave unexpectedly.
Fix:
- Check the user’s Profile/Permission Set to ensure they have Read access to
custom__GeoLocation__c. - Validate that your target
custom__Location__crecords have valid latitude/longitude values populated in the geolocation field.
4. Missing Input Null Checks
Your code directly accesses requests[0] without checking if the input list is empty or if the coordinates are null. If Flow passes an empty list (even though variables are marked required, edge cases can happen), this will throw an IndexOutOfBoundsException—and Flow wraps this into a generic validation error.
Fix:
Add basic guard clauses to handle edge cases gracefully:
public static List<custom__Location__c> getLocation(List<FlowInput> requests) { List<custom__Location__c> locList = new List<custom__Location__c>(); // Guard against empty/missing input if (requests == null || requests.isEmpty()) { return locList; } FlowInput input = requests[0]; if (input.coordlat == null || input.coordlng == null) { return locList; } // Updated SOQL with stable Record Type check locList = [SELECT Id, Name FROM custom__Location__c WHERE RecordType.DeveloperName = 'Synced' AND DISTANCE(custom__GeoLocation__c, GEOLOCATION(:input.coordlat, :input.coordlng), 'km') < 1 ORDER BY DISTANCE(custom__GeoLocation__c, GEOLOCATION(:input.coordlat, :input.coordlng), 'km') LIMIT 1]; return locList; }
5. Flow Input Variable Type Mismatch
Double-check that the variables you’re passing from Flow to the Apex action are Decimal type. If you’re using an Integer or Text variable for coordinates, the type conversion will fail, triggering a validation error.
Fix:
In Flow, confirm that coordlat and coordlng variables are set to type "Number" with Decimal places enabled.
6. Get Specific Error Details from Flow Debug
The fastest way to solve this is to get the actual error message, not the generic one. Here’s how:
- Open your Flow in Setup.
- Click the Debug button in the top-right corner, then select "Run Flow".
- Enter test coordinates that match what you used in Anonymous Apex.
- After the Flow fails, click the Execution Details link—this will show you the exact error (e.g., "Field is not accessible", "Index out of bounds", etc.) which will point you straight to the problem.
Start with the Flow debug step first—it’ll save you a ton of guesswork!
内容的提问来源于stack exchange,提问作者DomJH

