Create Routes for Specific Users
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
Signature
Map<String, Object> maps.API.StartAdvancedOptimizationForUsers(Set<Id> userIds, Date startDate)
- 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
Example
// Create a userIds variable to hold the Salesforce user IDs.
Set<Id> userIds = new Set<Id>();
// Create a variable for the current or future date.
Date today = Date.today();
// Check that each user is assigned to an active visit plan for specified date.
List<maps__AdvRouteTemplate__c> activeTemplates = [SELECT Id FROM maps__AdvRouteTemplate__c WHERE
maps__StartDate__c <= :today AND (maps__EndDate__c >= :today OR maps__IsRepeating__c = TRUE) AND
maps__Active__c = TRUE LIMIT 1];
// Query for the IDs of all users for which we want to generate routes.
for (User u : [SELECT Id FROM User WHERE Name IN ('User1, User2')]) {
userIds.add(u.Id);
}
// Call the method.
if (!activeTemplates.isEmpty() && !userIds.isEmpty()) {
maps.API.StartAdvancedOptimizationForUsers(userIds, 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
}
{
success: false
error: "Please provide either the current or future date in the request to complete optimization."
}
{
success: true,
warning: "No visit plans are set up for routing on the date you selected for the following users: [userId]"
}