Create a Route

The CreateRoute() Apex method creates a record that includes a list of waypoints that reps access in Salesforce Maps. The record doesn’t include a route between waypoints.

Signature 

1maps__Route__c maps.API.CreateRoute(map<string,object> options)

Where,

  • maps__Route__c is a Salesforce Maps route record containing a list of waypoints.
  • maps is the namespace that’s available after you install Salesforce Maps.
  • API is the class that contains the global methods exposed to developers.
  • CreateRoute() is the method.

Allocations 

This method requires 2–25 waypoints.

Sample Code 

This code returns a maps__Route__c record of waypoints of accounts with billing addresses in Atlanta.

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.

Warning

Input Format of Waypoints

1{
2version: '1', // Required. Version of the API endpoint. Must be '1'.
3name: 'String', // Name of the new route record.
4date: Date, // Time and date in Epoch format that the route is created for. Default is the current time and date.
5waypoints: [ {recordId:String, lat:Decimal, lng:Decimal, address:String, order: Integer, baseobjectid: String, markerlayerid: String, notes:String}, {...} ] // List of waypoint objects.
6}

Example

1// Create a list of waypoints.
2List<Map<String,Object>> waypoints = new List<Map<String,Object>>();
3
4// Retrieve the list of billing addresses from the database and
5// add them to the waypoints object.
6List<Account> accountList = [Select Id,BillingStreet,BillingCity,BillingState,BillingPostalcode, BillingLatitude,BillingLongitude From Account Where BillingCity = 'Atlanta' LIMIT 25];
7
8for(Integer i = 0; i < accountList.size(); i++){
9    Account thisAccount = accountList[i];
10    String address = thisAccount.BillingCity + ', ' + thisAccount.BillingState + ' ' + thisAccount.BillingPostalCode;
11    waypoints.add(new Map<String,Object>{
12        'lat' => thisAccount.BillingLatitude,
13        'lng' => thisAccount.BillingLongitude,
14        'address' => address,
15        'recordId' => thisAccount.id,
16        'baseobjectid' => 'a0N0t000002hEyvEAE',
17        'markerlayerid' => 'a0v0t000001wE91AAE',
18        'order' => i + 1
19    });
20}
21
22// Build the request.
23Map<String,Object> request = new Map<String,Object>();
24request.put('version','1');
25request.put('name','Atlanta Accounts Route');
26request.put('waypoints',waypoints);
27
28// Call the CreateRoute() method with the waypoints.
29maps__Route__c response = maps.API.CreateRoute(request);
30
31// Log the route record output in JSON format.
32system.debug(JSON.serialize(response));

Sample Response 

This method returns a maps__Route__c record, which inserts the waypoints that appear in Salesforce Maps. This JSON response illustrates the essential data stored in the route record.

1{
2  "attributes": {
3    "type": "maps__Route__c",
4    "url": "/services/data/v51.0/sobjects/maps__Route__c/a0y0t000001ht8HAAQ"
5  },
6  "Name": "Atlanta Accounts Route",
7  "maps__Date__c": "2021-05-04",
8  "Id": "a0y0t000001ht8HAAQ"
9}