HealthService User Experience
Use the HealthService API
HealthService Example
Compatibility and Requirements
Considerations and Limitations
Previous Versions
To develop a Lightning web component with health data features, use the HealthService API to read and write body-weight records on the device.
getWeight() and writeWeight() functions to read and write weight records.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.
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}It’s straightforward to read weight history using HealthService.
getWeight(), passing a GetWeightOptions object to filter by time range and limit the number of records.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.
To save a weight measurement to the device’s health store, construct a valid WeightRecord object and pass it to the writeWeight() function.
WeightRecord object in your JavaScript code with the weight value, unit, and time. This data often comes from a Salesforce record.writeWeight(), passing the WeightRecord object.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