Salesforce Field Service Integration with Route Optimization API

Prerequisites

To successfully integrate Salesforce Field Service with NextBillion.ai, ensure you have the following prerequisites in place:

  1. Salesforce Field Service Account Credentials: You will need valid credentials, including a username and password, to access your Salesforce Field Service account.

  2. NextBillion.ai's API Key: Obtain an API key from NextBillion.ai, which serves as the authentication mechanism for accessing their services and data. This key is essential for secure and authorized communication between your systems and NextBillion.ai's APIs.

Generating Access Token

  • Create a Salesforce Account: Begin by setting up a Salesforce account if you haven't already.

  • Create a Custom App: Once your Salesforce account is established, create a custom application within the Salesforce platform.


    documentation image
  • Configure OAuth for Connection: Configure OAuth settings to establish a secure connection. Before testing the connection, keep these important considerations in mind:

    • Specify Callback URL: Ensure that you specify a valid callback URL.

    • Retrieve Consumer Key and Secret: Obtain your Consumer Key and Secret, which are essential for authentication.

    documentation image
  • Test the API Connection: Use Postman or a similar tool to test the API connection and generate the access token.


    documentation image
  • Generate API Access Token: After testing the connection successfully, generate the API access token.

  • Verify Basic CRUD Operations: Finally, verify that basic CRUD (Create, Read, Update, Delete) operations are functioning as expected using the generated access token.

Populate Data from Salesforce

To enrich your Salesforce application, you need to populate the following key components:

Service Appointments

Import and integrate service appointment data from your Salesforce application.

documentation image

Sample API Request

When the following cURL command is executed, it sends a GET request to Salesforce, which responds with the results of the specified query, typically in JSON format. This allows you to retrieve Salesforce data that matches the criteria defined in the query, which can then be used for integration purposes.

1
# Fetch addresses from Salesforce
2
curl --request GET \
3
--url https://nextbillionai-dev-ed.my.salesforce.com/services/data/v58.0/query?q=SELECT+AccountId,Address,AppointmentNumber,Id__c +FROM+ServiceAppointment

Sample API Response

1
{
2
"totalSize": 5,
3
"done": true,
4
"records": "[...]"
5
}

The integrated dashboard will resemble the following image:

documentation image

Service Resources

Likewise, ensure that service resource information is efficiently integrated and populated within your Salesforce environment. Retrieve a list of your Technicians, which represent your vehicles. This information will be used to optimize routes based on vehicle capacity and other parameters.

documentation image

Sample API Request

1
curl --request GET \
2
--url https://nextbillionai-dev-ed.my.salesforce.com//services/data/v58.0/query?q=SELECT+id__c,Name+FROM+ServiceResource
3
documentation image

Sample API Response

1
{
2
"totalSize": 2,
3
"done": true,
4
"records": "[...]"
5
}

After successfully populating the data from Salesforce, your integerated dashboard will look something like this:

documentation image

Integration with Route Optimization API

After successfully pulling the necessary data from Salesforce, integrate the fetched data with Route Optimization using the following API endpoint.

Map the fetched Salesforce data to the respective API parameters. Configure optimization settings such as shift start and end time, service time, and start and end location coordinates.

1
let vehicles = [];
2
let shifts = [];
3
let starts = [];
4
let ends = [];
5
let veh_attr = [];
6
let shiftStart = (new Date(dtShiftStart2.value).getTime()/1000);
7
let shiftEnd = (new Date(dtShiftEnd2.value).getTime()/1000);
8
tblStops.data.forEach(planstop => {
9
//
10
})

Once you have successfully retrieved the required data from Salesforce, the next step is to seamlessly integrate this data with the Route Optimization service using the provided API.

Follow these steps to achieve a successful integration:

Step 1: Define Data Structures

Begin by setting up the necessary data structures to facilitate the integration. These include arrays and variables for vehicles, shifts, start and end locations, vehicle attributes, and stop attributes.

1
let vehicles = [];
2
let shifts = [];
3
let starts = [];
4
let ends = [];
5
let veh_attr = [];
6
let stop_attr = [];

Step 2: Configure Optimization Settings

Specify optimization settings to tailor the routing process to your specific needs. These settings typically include shift start and end times, service times, and the coordinates of start and end locations.

1
let shiftStart = (new Date(dtShiftStart2.value).getTime() / 1000);
2
let shiftEnd = (new Date(dtShiftEnd2.value).getTime() / 1000);

Step 3: Prepare Data for Route Optimization

Assemble the data required for route optimization. This involves creating arrays for locations, jobs, and shipments. Additionally, you'll populate the vehicle and job attributes as needed.

1
let vehicles = [];
2
let shifts = [];
3
let starts = [];
4
let ends = [];
5
let veh_attr = [];
6
let shiftStart = (new Date(dtShiftStart2.value).getTime()/1000);
7
let shiftEnd = (new Date(dtShiftEnd2.value).getTime()/1000);
8
tblStops.data.forEach(planstop => {
9
//
10
})

Step 4: Store Data and Trigger Optimization

Store the prepared data structures in local storage for future reference and initiate the route optimization process.

1
localStorage.setValue('vehiclesNBAI', vehicles);
2
localStorage.setValue('shipmentsNBAI', shipments);
3
localStorage.setValue('jobsNBAI', jobs);
4
localStorage.setValue('locationsNBAI', locations);
5
localStorage.setValue('nbaiOptions', options);
6
localStorage.setValue('nbaiVRPResult', null);
7
8
nbaiOptimizationRun.trigger();

By following these steps, you can effectively connect and integrate Salesforce data with the Route Optimization API, ensuring efficient routing and resource allocation for your tasks or deliveries.

Create Jobs with Route Optimization API

This integration step showcases how to utilize NextBillion.ai's Route Optimization API to create optimization jobs, allowing for efficient route planning and resource allocation. The API response provides critical job information, enabling further interaction and tracking within the integration workflow.

POST Query

To create optimization jobs, a POST request is sent to the designated API endpoint using the curl command-line tool or an equivalent HTTP client.

1
curl --request POST \
2
--url https://api.nextbillion.io/optimization/v2?key={{localStorage.values.apiKey}}

API Request

1
{
2
"request": {
3
"url": "https://api.nextbillion.io/optimization/v2?key=<your api key>",
4
"method": "POST",
5
"body": "{...}",
6
"headers": "{...}"
7
},
8
"response": "{...}"
9
}

The request payload contains essential information needed for the optimization job. It is structured as a JSON object and includes the following elements:

  • locations: Specifies location data, including IDs, descriptions, coordinates, and time windows.

  • jobs: Defines job details, such as IDs, descriptions, location indices, service times, and time windows.

  • vehicles: Describes vehicle properties, including IDs, starting indices, time windows, and task capacities.

  • options: Specifies optimization settings, such as routing mode.

  • result: Initially set to null, as it awaits the response from the API.

API Response

Upon successfully processing the request, the API responds with a JSON object containing the following details:

1
{
2
"id": "456fd9f88e72cf92aec1bd68cfa81cea",
3
"message": "Optimization job created",
4
"status": "Ok"
5
}

Retrieving Optimized Route Result

To obtain the optimized route results after initiating a route optimization job, you can utilize the provided GET query. This query enables you to fetch the optimized route details from the Route Optimization API.

GET Query

1
https://api.nextbillion.io/optimization/v2/result?id={{nbaiOptimizationRun.data.id}}&key={{localStorage.values.apiKey}}

In the above URL, two query parameters are used:

  • id: This parameter specifies the unique identifier associated with the optimization job. It is obtained from the nbaiOptimizationRun.data.id variable.

  • key: The localStorage.values.apiKey variable provides the API key necessary for authentication.

Example JSON Response

Upon making the GET request, the API will respond with a JSON object containing the optimized route details. Here's an example of what the JSON response may look like:

1
{
2
"result": {
3
"code": 0,
4
"summary": "{...}",
5
"routes": "[...]"
6
},
7
"status": "Ok",
8
"message": ""
9
}

Accessing Optimized Routes in NextBillion.ai’s Optimizer

To visualize and analyze the optimized routes generated by NextBillion's Optimizer, you can use the following URL.

1
https://playground.nextbillion.ai/optimization-tester?apiKey={{localStorage.values.apiKey}}&requestID={{nbaiOptimizationRun.data.id}}

The above URL will take you to NextBillion's Optimizer interface where you can explore and interact with the optimized route results associated with your specific request ID and API key. This provides a convenient way to gain insights into your optimized routes for enhanced decision-making and resource management

documentation image

By integrating Salesforce Field Service with NextBillion.ai's Route Optimization API, businesses can streamline field operations, reduce costs, and provide better service to customers. This integration enhances efficiency and competitiveness in the market, ultimately leading to improved customer satisfaction.

© 2024 NextBillion.ai all rights reserved.