maps is the namespace that’s available after you install Salesforce Maps.
API is the class that contains the global methods exposed to developers.
Geocode() is the method.
Sample Code
This code returns the geographical coordinates of the Salesforce headquarters. The output also returns the formatted address and adds missing information, such as postal code (ZIP+4 for U.S. addresses) and country, if available.
If you invoke methods within a flow, process builder, or trigger, avoid uncommitted work errors when you perform one of the following.
Call the methods through a future method
Call the methods as queueable
Uncommitted work errors can also arise if calling out to Salesforce Maps methods more than one time in a single transaction and the Salesforce Maps token hasn’t refreshed in the last 24 hours. If these errors occur, separate each callout into individual transactions.
Different processes refresh the token, such as plotting a route or schedule. The refresh process is almost immediate after each qualifying action occurs.
1// Create and add an address.2String salesforceAddress = '415 Mission Street, San Francisco, CA 94105 USA';34Map<string,object>options = new Map<String,Object>{5 'version' =>'1', // Required. Version of the API endpoint. Must be '1'.6 'address' =>salesforceAddress7};89// Call the Geocode() method with the address.10Map<String, Object>response = maps.API.Geocode(options);1112// Log the resulting geographical coordinates and formatted address. 13system.debug(response);
Sample Response
Although the return value is an Apex Map<String, Object> object, this JSON response illustrates the essential data you receive in the resulting map.
If you invoke this method within a flow, process builder, or trigger and want to use the data from the JSON response, implement logic to retrieve that data. For example, you want to save the latitude and longitude coordinates from the JSON response to your records.
The postal field returns postal code in ZIP+4 format (for example, 94105-1234).
If an address can’t be geocoded, you receive this response. Although the return value is an Apex Map<String, Object> object, this JSON response illustrates the essential data you receive in the resulting map.
1{2 "success": false,3 "message": "No results for that particular address."4}