Use the ContactsService API

To develop a Lightning web component with contacts-based features, use the ContactsService API as your method for selecting contacts from a device’s address book.

  1. Import ContactsService into your component definition to make the ContactsService API functions available to your code.
  2. Test to make sure ContactsService is available before you call contacts-related functions.
  3. Use the getContacts() function to select and access contacts.

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

After it’s imported into your component, use the factory function to get an instance of ContactsService. With your ContactsService instance, use the utility functions and constants to verify availability. Then use contacts-related functions to select contacts from the device.

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

It’s straightforward to create a custom contacts processing feature using ContactsService.

  1. Open the contacts selection list view using getContacts().
  2. Your user selects contacts from the list.
  3. Process the results (the contacts data for the selected contacts).

For example:

See getContacts(options) for more details about how to handle contacts data, handle errors, and so on.

To save a contact to the device’s address book, construct a valid Contact object and pass it to the putContact() method.

  1. Create a Contact object in your JavaScript code with the desired details (name, phone, email, etc.). This data often comes from a Salesforce record.
  2. Call putContact(), passing the Contact object.
  3. Process the result, which is a Contact object of the successfully saved record, or handle the error.

For example:

See the ContactsService API reference for full details about the Contact object structure.

See Also