Use the HealthService API

To develop a Lightning web component with health data features, use the HealthService API to read and write body-weight records on the device.

  1. Import HealthService into your component definition to make the HealthService API functions available to your code.
  2. Test to make sure HealthService is available before you call health data functions.
  3. Use the getWeight() and writeWeight() functions to read and write weight records.

Add HealthService to a Lightning Web Component 

In your component’s JavaScript file, import HealthService using the standard JavaScript import statement. Specifically, import the getHealthService() factory function from the lightning/mobileCapabilities module, like so:

1import { getHealthService } from "lightning/mobileCapabilities";

After it’s imported into your component, use the factory function to get an instance of HealthService. With your HealthService instance, use the utility functions and constants to verify availability. Then use the health data functions to read and write weight records on the device.

Test HealthService Availability 

HealthService depends on physical device hardware and platform features. A component that uses HealthService renders without errors on a desktop computer or in a mobile browser, but health data functions fail. To avoid these errors, test if HealthService functionality is available before you use it.

1handleGetWeightClick(event) {
2    const myHealthService = getHealthService();
3    if(myHealthService.isAvailable()) {
4        // Perform health data operations
5    }
6    else {
7        // HealthService not available, or consuming app hasn't implemented it
8        // Not running on a device with a health store, etc.
9        // Handle with message, error, beep, and so on
10    }
11}

Read Weight Records from the Device 

It’s straightforward to read weight history using HealthService.

  1. Read weight records with getWeight(), passing a GetWeightOptions object to filter by time range and limit the number of records.
  2. Process the results, which are returned as an array of WeightRecord objects.

For example:

1// Read weight records, and then process them
2const options = {
3  startTime: null, // null = no lower bound
4  endTime: null, // null = now
5  limit: 10, // return at most 10 records
6  preferredUnit: "kg", // return values in kilograms
7};
8myHealthService
9  .getWeight(options)
10  .then((results) => {
11    // Do something with the weight data
12    this.weightRecords = results;
13    console.log(results);
14  })
15  .catch((error) => {
16    // Handle permission or read errors here
17    this.weightRecords = [];
18    console.error("Error code: " + error.code);
19    console.error("Error message: " + error.message);
20  });

See getWeight(options) for more details about how to handle weight data and errors.

Write a Weight Record to the Device 

To save a weight measurement to the device’s health store, construct a valid WeightRecord object and pass it to the writeWeight() function.

  1. Create a WeightRecord object in your JavaScript code with the weight value, unit, and time. This data often comes from a Salesforce record.
  2. Call writeWeight(), passing the WeightRecord object.
  3. Handle the result to confirm that the write succeeded, or handle the error.

For example:

1// First, create a weight record from your source data
2const record = {
3  value: 80.5,
4  unit: "kg",
5  startTime: Date.now(), // epoch milliseconds
6  endTime: null,
7};
8// Now, write the record to the device
9myHealthService
10  .writeWeight(record)
11  .then(() => {
12    // The weight record was successfully written
13    console.log("Weight record saved.");
14  })
15  .catch((error) => {
16    // Handle errors here, such as the user denying permission or a failure to write
17    console.error("Error code: " + error.code);
18    console.error("Error message: " + error.message);
19  });

See writeWeight(record) for full details about the WeightRecord object structure.

See Also