Create Routes from Visit Plan

The StartAdvancedOptimizationForVisitPlan() Apex method creates routes for all users assigned to a 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 StartAdvancedOptimizationForVisitPlan() Apex method is the equivalent of clicking Plan Visits on an active visit plan in Maps Advanced Visit Plans.

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.

Signature

Map<String, Object> maps.API.StartAdvancedOptimizationForVisitPlan(Id templateId, Date startDate)
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.
  • StartAdvancedOptimizationForVisitPlan() is the method.
  • templateId is the Salesforce record id of a visit plan.
  • 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, avoid uncommitted work errors when you perform one of the following.

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

Different processes refresh the token, such as plotting a route or schedule. The refresh process is almost immediate after each qualifying action occurs.

Warning

Example

// Create a variable for the current or future date.
Date today = Date.today();

// Query for the record ID of an active visit plan.
List<maps__AdvRouteTemplate__c> activeTemplates = [SELECT Id FROM maps__AdvRouteTemplate__c WHERE Name = 'NAME_OF_VISIT_PLAN' AND 
maps__StartDate__c <= :today AND (maps__EndDate__c >= :today OR maps__IsRepeating__c = TRUE) AND 
maps__Active__c = TRUE LIMIT 1];

// Call the method.
if (!activeTemplates.isEmpty()) {
maps.API.StartAdvancedOptimizationForVisitPlan(activeTemplates[0].Id, today);
}

Sample Response

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

{
    success: true
}
If the routes weren’t created successfully, an error message is returned. In this context, optimization refers to creating routes.
{
    success: false
    error: "Please provide either the current or future date in the request to complete optimization."
}