Newer Version Available

This content describes an older version of this product. View Latest

Create Routes for Specific Users

The StartAdvancedOptimizationForUsers() Apex method creates routes for users assigned to any active Salesforce Maps Advanced visit plan. Use this method to leverage Salesforce Maps Advanced visit planning from your custom workflow or app, such as a retail execution app. By using Apex, you can generate routes for reps automatically without having to make field reps click through the UI to manually generate routes.

The StartAdvancedOptimizationForUsers() Apex method is the equivalent of clicking Plan My Visits for users in Maps Advanced Route.

When you use this method, only the user’s future shifts are included for planning visits along the route. If a user’s shift has started or is within 2 hours of starting at the time the method runs, the method doesn’t change visit appointments for today’s date. In that case, one day is added to the starting date (startDate).

Visits are created from startDate through a visit plan’s end date. If a user is assigned to more than one visit plan, then visits are planned for the length of time dictated by the visit plan with the end date furthest in the future.

Example

A user is assigned to consecutive plans Visit Plan A and Visit Plan B. At the time the method is run, Visit Plan A has 10 days remaining until its end date. Visit Plan B has a start date immediately after Visit Plan A’s end date, and has 30 days remaining until its end date. When the method runs, visits are scheduled for the user for the remainder of Visit Plan A (10 days), and scheduled for 20 days in Visit Plan B, for a total of 30 days.

Signature

Where,
  • maps is the namespace that's available after you install Salesforce Maps.
  • API is the class that contains the global methods exposed to developers.
  • StartAdvancedOptimizationForUsers() is the method.
  • userIds is a set of IDs of the Salesforce users for which you want to generate routes. Every user must be assigned to an active visit plan for the date you specify in startDate.
  • startDate is optional. If not specified, the method uses today’s date as the date to start planning visits. If startDate occurs during a user’s shift or within 2 hours of the user’s next shift, then one day is added to the specified startDate.

Sample Code

If you invoke methods within a flow, process builder, or trigger, do one of the following to avoid uncommitted work errors:

  • Call the methods through a future method
  • Call the methods as queueable

Warning

Example

Sample Response

This method returns an Apex Map<String, Object> object that contains a boolean flag indicating whether the routes were created successfully.

If the routes weren’t created successfully, an error message is returned. In this context, optimization refers to creating routes.
If the routes were created successfully for some users, but not others, a warning is returned. In this example, some users weren’t assigned to an active visit plan for the startDate, so those users were skipped.