Mobile Offline Developer Guide - Summer 23
Mobile Offline Developer Guide - Summer 23
PREVIEW
Note: This release is in preview. Features described in this document don’t become generally available
until the latest general availability date that Salesforce announces for this release. Before then, and @salesforcedocs
where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability Last updated: March 29, 2023
within any particular time frame or at all. Make your purchase decisions only on the basis of generally
available products and features.
© Copyright 2000–2023 Salesforce, Inc. All rights reserved. Salesforce is a registered trademark of Salesforce, Inc., as are other
names and marks. Other marks appearing herein may be trademarks of their respective owners.
CONTENTS
Chapter 1: Offline App Development with Lightning Web Components and LWC
Offline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
About This Developer Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
LWC Offline Enabled Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Learn Lightning Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Learn Lightning Web Components with Trailhead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Learn Lightning Web Components with Documentation . . . . . . . . . . . . . . . . . . . . . . . . 3
1
Offline App Development with Lightning Web Components About This Developer Guide
and LWC Offline
IN THIS SECTION:
Learn Lightning Web Components with Trailhead
If you’re not already an experienced LWC developer, the best way to learn Lightning web components is with the extensive collection
of lessons and projects on Trailhead.
Learn Lightning Web Components with Documentation
Use the Lightning Web Component Developer Guide to understand the Lightning Web Components framework and how to use it
with Salesforce.
2
Offline App Development with Lightning Web Components Learn Lightning Web Components with Documentation
and LWC Offline
Note: Most LWC modules and projects are general purpose, rather than specific to a particular mobile app. For setting up to
specifically work with Lightning web components and your specific mobile app, such as the Field Service mobile app, follow the
steps provided in Set Up Your Development Environment on page 35.
• Quick Start: Lightning Web Components
• Set Up Your Salesforce Mobile Developer Tools for Lightning Web Components
• Set Up Your Lightning Web Components Developer Tools
3
Offline App Development with Lightning Web Components Learn Lightning Web Components with Documentation
and LWC Offline
The Lightning Web Component Developer Guide is the canonical resource for all details of developing with Lightning web components.
After you’ve learned the conceptual basics, you continue to use the developer guide to find answers to specific “how do I...?”-type
questions. The developer guide also includes the Component Reference, which provides reference documentation for all of the built-in
base components available in the framework.
Mobile-specific documentation for Lightning web components includes the following:
• Preview Lightning Web Components on Mobile (full details on development tools)
• Create Mobile-Ready Components (guidelines and best practices)
• Use Mobile Device Features in Mobile Apps (add platform native features to LWCs)
While not specific to Lightning web components or LWC Offline, the following documentation can be helpful during your development
efforts.
• Field Service Developer Guide
• Field Service Mobile App Salesforce Help
• Briefcase Builder Salesforce Help
• Quick Actions Salesforce Help
• Salesforce Mobile Debugging Tools
• Field Service Mobile LWCs Trailblazer community
• Mobile Automated Testing Trailblazer community
4
CHAPTER 2 Considerations and Limitations
In this chapter ... LWC Offline is designed to let you build great apps that can function without a network connection, but
it’s not the full Salesforce service. Lightning web components have a number of limitations when used
• General offline, including missing capabilities, reduced performance, and software defects (bugs). Keep these
Considerations limitations in mind as you design and develop your offline customizations.
• Considerations for
Field Service Mobile
App
• Base Components
Support
• Modules Support
• Wire Adapters
Support
• Entity Support
• Metadata and
Custom Metadata
Types Support
5
Considerations and Limitations General Considerations
General Considerations
Consider these general details when planning your Lightning web components development efforts.
• The Lightning Web Components framework includes a wide range of built-in components, a number of modules that enable features,
and a range of wire adapters for data access. The implementation available in LWC Offline supports a subset of these features. See
additional details elsewhere in this guide.
• You can only use Lightning web components that are used as global or object-specific quick actions.
• Object-specific quick actions can only be added to record detail pages.
• Your components must be Lightning web components.
– You can’t use Aura-based Lightning components, despite the similar name.
– You can’t use Visualforce at all with Lightning web components.
6
Considerations and Limitations Base Components Support
• There’s a conflict between Appointment Assistance and LWC Offline that can result in missing URLs. See this known issue for details.
• (iOS only) Changes to Lightning web components are loaded into the app only when the app is fully quit and then relaunched (a
“cold start”).
lightning-datatable This complex component was never designed for mobile use, and
its behavior in mobile contexts is subject to change without notice.
See this post in the Trailblazer community for sample code for a
simplified, mobile-friendly data table.
lightning-file-upload
lightning-formatted-address The static map feature only works in online mode. If a user is offline,
an error message is displayed instead.
lightning-formatted-rich-text URLs don’t open within a navigation view; external URLs work, but
links to records don’t. Additionally, you can’t open any URL with a
target="_blank" attribute.
lightning-formatted-url URLs don’t open within a navigation view; external URLs work, but
links to records don’t. Additionally, you can’t open any URL with a
target="_blank" attribute.
lightning-input-address The address lookup feature only works in online mode. If a user is
offline, an error message is displayed instead.
7
Considerations and Limitations Base Components Support
lightning-pill URLs don’t open within a navigation view; external URLs work, but
links to records don’t.
lightning-pill-container URLs don’t open within a navigation view; external URLs work, but
links to records don’t.
8
Considerations and Limitations Modules Support
lightning-tree-grid
SEE ALSO:
Component Reference
Modules Support
Lightning web component modules in the lightning namespace are described in the Component Reference in the Lightning Web
Components Developer Guide. Modules scoped with @salesforce are described in @salesforce Modules in the Lightning
Web Components Developer Guide. All modules are being reviewed for correct behavior in LWC Offline-enabled mobile apps.
The following table presents current findings.
•
— Supported. Expected to behave as documented.
•
— Limited Support. Can be used, but has known (and possibly unknown) issues.
•
— Not Supported. Doesn’t work, or shouldn’t be used.
lightning/alert
Create an alert modal within your component.
lightning/confirm
Create a confirm modal within your component.
9
Considerations and Limitations Modules Support
lightning/flowSupport
Provides events to control flow navigation and
notify the flow of changes in attribute values.
lightning/navigation Available for use, but supported page reference types are limited.
Generates a URL or navigates to a page reference. See Navigation for details.
lightning/platformShowToastEvent You can import this module and fire toast events. However, toast
Displays toasts to provide feedback to a user messages aren’t handled or displayed. Use lightning/alert,
following an action, such as after a record is lightning/confirm, or lightning/prompt instead.
created.
lightning/prompt
Create a prompt modal within your component.
@salesforce/apex See Use Apex While Mobile and Offline for usage details.
Import Apex methods as functions that a
component can call either via @wire or
imperatively.
@salesforce/apexContinuation See Use Apex While Mobile and Offline for usage details.
10
Considerations and Limitations Modules Support
@salesforce/client/formFactor This module “works,” but always returns Small when used in an
Import a name that refers to the form factor of LWC Offline-enabled app.
the hardware running the app.
@salesforce/contentAssetUrl
Import content asset files.
@salesforce/i18n
Import internationalization properties.
@salesforce/label
Import labels defined in your Salesforce
organization.
@salesforce/resourceUrl
Import static resources defined in your Salesforce
organization.
@salesforce/schema
Import references to Salesforce objects and fields
defined in your org.
@salesforce/user
Import the current user’s ID.
@salesforce/userPermission
Import a permission and check whether it’s
assigned to the current user.
11
Considerations and Limitations Wire Adapters Support
SEE ALSO:
Component Reference
Lightning Web Components Developer Guide: @salesforce Modules
createRecord Drafts-Enabled
createRecordInputFilteredBy
EditedFields
deleteRecord Drafts-Enabled
generateRecordInputForCreate
generateRecordInputForUpdate
getFieldValue
getFieldDisplayValue
12
Considerations and Limitations Wire Adapters Support
getRecords Drafts-Enabled
getRecordCreateDefaults Offline-Supported
getRecordNotifyChange
updateRecord Drafts-Enabled
getObjectInfo Offline-Supported
getObjectInfos Offline-Supported
getPicklistValues Offline-Supported
getPicklistValuesByRecordType Offline-Supported
13
Considerations and Limitations Wire Adapters Support
getListInfosByName
getRelatedListRecordsBatch
getRelatedListInfo Offline-Supported
getRelatedListInfoBatch Offline-Supported
getRelatedListsInfo Offline-Supported
14
Considerations and Limitations Wire Adapters Support
Note: Forward looking statement: Our goal is to provide supported alternatives to getRecordUi and getListUi. If you
limit your usage of these adapters today, you’ll have an easier time migrating later.
• In the case of getRecordUi, limit yourself to getting layout metadata details, and use the data-only adapters getRecord and
getRecords for data access.
• In the case of getListUi, use the new getListInfoByName wire adapter to get list view metadata. If you must use
getListUi to access list view records, see the following considerations.
Considerations for List Adapters
List- or collection-oriented adapters such as getListUi, getRelatedListRecords, and getRelatedListCount have
limited support for offline updates. Specifically, list logic isn’t re-evaluated for changes made while you’re offline. That is, if you create or
modify a record offline and it falls into or out of the list criteria, the record isn’t added or removed from the list until you’re back online.
The list updates only after the changes sync back to Salesforce. This limitation affects list membership, but does not affect the display of
records that are a part of a list.
Here’s an example to make this clear. Let’s say you use getRelatedListRecords as a source to display a list of records, and the
related list criteria limits list membership to accounts whose name begins with “A”. While offline, if you update one of those records to
change an account name to begin with a “B”, from “Apple” to “Banana”, that record will still display in the list, with the updated account
name “Banana”. Once you return online, the change syncs to Salesforce, and the list criteria is reevaluated. The Banana account will no
longer be a member of the related list, and the wire adapter is updated, triggering a component refresh. The list of records returned by
getRelatedListRecords won’t include the record for the Banana account, and it will disappear from the list displayed in your
component’s user interface.
Considerations for getRecordUi
getRecordUi is affected by numerous issues when used while offline.
• Invoking getRecordUi on a draft record that was created while offline returns an error.
• If you edit a record such that its layout changes—for example, by changing the record type—the results of invoking getRecordUi
on that record can be inconsistent.
• If you change a relationship field on a record, and the new relationship references a record with a different object type or record
type, the results of invoking getRecordUi on that record can be inconsistent or result in an error.
In theory, if you’re able to limit changes to records while offline to the scalar (non-relationship, non-metadata affecting) fields of that
record, then invoking getRecordUi on that record should work as documented. In practice this is challenging, and when you miss
it results in inconsistent or incorrect behavior that can be hard to troubleshoot. If you must use it, exercise extreme caution.
getRecordUi is used in the implementations of the following Lightning base components, causing them to have similar limitations:
• lightning-record-form
• lightning-record-edit-form
• lightning-record-view-form
15
Considerations and Limitations Entity Support
errors = [ errors ];
}
SEE ALSO:
Lightning Web Components Developer Guide: lightning/ui*Api Wire Adapters and Functions
Entity Support
LWC Offline uses the UI-API to access entity data. The UI-API supports a long list of standard entities, and all custom entities.
For a complete list of supported entities, see Supported Objects in the User Interface API Developer Guide.
Note: The ContentDocument, ContentVersion, and associated entities, used as part of file uploading features, aren’t fully supported
at this time. This limitation affects all base components that provide file uploading functionality. See Upload Photos from LWCs
Using lightning-input Base Component in the Trailblazer community for additional details and a partial work-around.
The following entities aren’t fully supported by UI-API, but are commonly used in Field Service. Support for them is incomplete.
• Case (not supported in related lists)
• LinkedArticleTask (recurrence isn’t supported)
• WorkOrder (not supported in related lists)
• WorkOrderStatus (not supported in related lists)
• WorkOrderLineItem (not supported in related lists)
SEE ALSO:
User Interface API Developer Guide: Supported Objects
User Interface API Developer Guide
SEE ALSO:
User Interface API Developer Guide
16
CHAPTER 3 Offline Considerations
In this chapter ... The offline environment presents a number of technical challenges for LWC Offline-enabled mobile
apps, and for your Lightning web components.
• What Happens When
Something Isn’t
The Salesforce Field Service mobile app is heavily optimized for offline use. These optimizations are
Primed (Preloaded) enabled by our complete understanding of how the built-in features are implemented, including code,
data, and metadata relationships. The app contains or loads the information it needs for built-in features
• Create Components
to perform well, online and offline. See Offline Priming in the Field Service Mobile App in Salesforce Help
with Offline Analysis
for more details.
In Mind
• Determine Online or The Field Service mobile app doesn’t, and can’t, have this same level of knowledge about your custom
Offline Status features built with Lightning web components. Instead, the app analyzes your custom objects, page
layouts, components, and other metadata, and then loads the data and metadata it thinks you need. To
improve the quality of this analysis, and thus the performance of your components, you must follow a
number of guidelines when developing your Lightning web components.
17
Offline Considerations What Happens When Something Isn’t Primed (Preloaded)
Tip: See Validate Lightning Web Components for Offline Use to install validation tools that provide support within VS Code for
the previous guidelines.
During initial priming when the app loads, the “current page” can’t be known. This means that resources associated with that page —
layout metadata, object and field metadata, record data — can’t be fully determined in advance. And so the component can’t be primed
completely.
18
Offline Considerations Determine Online or Offline Status
The workaround in this case, and in general, is to find another way to make these dependencies explicit, instead of implicitly defined by
a reference that can’t be resolved until runtime. Referencing a specific recordId, recordTypeId, or apiName provides enough
information to determine the dependencies without the specific page context.
19
CHAPTER 4 Use Salesforce Features While Offline
In this chapter ... Although LWC Offline is intended to “just work” when you use features while offline, there are nuances
and additional considerations for using some features while offline. This chapter provides details for how
• Use Apex While to use these features effectively in LWCs to make them offline-ready.
Mobile and Offline
• Use Images in an
LWC Offline-Enabled
Component
• Navigation
20
Use Salesforce Features While Offline Use Apex While Mobile and Offline
Important: Before you make plans to reuse existing Apex custom code in your offline features, read important details about
offline caching for Apex in Apex Results Are Separate from Other Primed Data.
IN THIS SECTION:
Use Apex in Lightning Web Components While Online
The essentials of using Apex within Lightning web components are described in "Call Apex Methods" in the Lightning Web Components
Developer Guide. While Apex features behave as documented when a client device is online, there are additional features available
within an offline-enabled mobile app.
Apex Results Are Separate from Other Primed Data
Apex results are saved in the durable store separately from data stored by built-in components, modules, and wires that use
Lightning Data Service (LDS) to access data.
Additional Considerations for Apex in an Offline-Enabled Mobile App
The following differences in behavior compared to Apex run from a browser-based connection apply to Apex when used in Lightning
web components in an offline-enabled mobile app.
Understand Apex Behavior While Offline
Additional features that are built into offline-enabled apps allow the app, including Lightning web components and even Apex, to
continue to function. Knowing these features, and their limitations, is critical to writing LWCs that function well, even without a
connection to the Salesforce service.
Additional Documentation for Apex in Lightning Web Components
Learn more about how to use Apex, including continuations, from Lightning web components documentation resources.
21
Use Salesforce Features While Offline Use Apex in Lightning Web Components While Online
IN THIS SECTION:
Reactive Apex Wires
Reading data via a wire adapter is the “natural” way to access data in Lightning web components. To add a read-only Apex method
to a Lightning web component, first import the Apex method from the @salesforce/apex module, and then use the @wire
annotation to connect that method to a property or function in your component.
Imperative Apex
Imperative Apex is the more traditional way to call an Apex method, as a network-based API call. Imperative Apex allows you to
control exactly when the method is called. You’re in control of the invocation, rather than the framework.
Imperative Apex
Imperative Apex is the more traditional way to call an Apex method, as a network-based API call. Imperative Apex allows you to control
exactly when the method is called. You’re in control of the invocation, rather than the framework.
Imperative Apex is more flexible—and less restrictive—than reactive Apex wires. For example, you can use imperative Apex calls to
change data on the server. The full details are described in Call Apex Methods Imperatively in the Lightning Web Components Developer
Guide.
However, imperative Apex has a significant limitation in an offline-enabled mobile app. You can’t use imperative Apex while offline.
This is by design, since imperative Apex is allowed to change data on the server. There’s no way to reconcile what might change on the
server side with data cached on the client side. This is because the client has no knowledge of the implementation of a server-side Apex
method. See Imperative Apex While Offline.
22
Use Salesforce Features While Offline Apex Results Are Separate from Other Primed Data
Data that’s loaded during priming activities, or that’s loaded using base components, wire adapters, and other built-in features of
Lightning web components, is stored in one consolidated cache. In an offline-enabled mobile app, it’s also saved to the durable store
for offline use. As you load new data, it’s merged into the local store of cached data. This merging includes updating existing items with
changes loaded from Salesforce.
The mechanism that makes this possible is Lightning Data Service, or LDS. LDS results include metadata about the returned results. This
metadata, sometimes referred to as ObjectInfo, allows LDS to treat the results as real Salesforce objects.
Apex requests are different. While Apex methods can return nothing more than records, there’s nothing that ensures this. Apex requests
can return arbitrary data, in any format you build in your Apex code. As a result, Apex methods don’t return ObjectInfos for the data
contained in the response. And without the ObjectInfos for the results, there’s no way to treat those results as Salesforce records—and
therefore no way to merge them in with records saved locally by LDS.
To simplify the actual implementations, think of LDS results as being records that are indexed by record ID, with each record stored in
a separate representation of a Salesforce object. In contrast, Apex results are stored as one big opaque blob per request, indexed on the
request URL, including parameters.
While you can interpret structured data inside that opaque blob, LDS and the LWC framework can’t. If you load the same records with
LDS and Apex, those records are stored separately, as duplicates. Updates to one copy don’t affect the other, unless you fire the Apex
request again (to load changes performed via LDS), or tell the framework to reload stale LDS data (to load changes performed via Apex).
This poses a challenge for implementing features that use both the built-in data access features of Lightning Web Components and the
fully customizable logic of Apex. Our advice is to use the base Lightning components whenever possible, then use LDS wire adapters,
and then if necessary, use Apex. An extensive discussion of this tiered strategy can be found in Data Guidelines in the Lightning Web
Components Developer Guide.
Note: We recognize that LDS and built in components don’t cover every use case, and that there’s always a place for custom
logic performed on the server, especially for transactional operations. We intend to improve the capabilities of LDS over time,
including adding advanced capabilities for complex queries and other operations. We advise planning your use of Apex in such
a way that you can refactor your data access logic to adopt new LDS features as they become available.
23
Use Salesforce Features While Offline Additional Considerations for Apex in an Offline-Enabled
Mobile App
IN THIS SECTION:
Apex Wires While Offline
Lightning web components that wire properties or functions to Apex methods continue to provide cached values from the durable
store, if available.
Imperative Apex While Offline
Imperative Apex calls always fail when the client device is offline. When using imperative Apex in an offline-enabled mobile
app, it’s essential to handle the possibility of a network failure error.
Refresh Records Cached in Durable Store While Offline
While it’s not possible to retrieve updated data from Salesforce while a client device is offline, it’s still possible to request updates
when data is known to be stale.
24
Use Salesforce Features While Offline Understand Apex Behavior While Offline
When using a property, the wire service either provisions the results to the <property>.data property, or returns an error to the
<property>.error property. Use an if:true directive in your component template to check for the presence of each, and
render the appropriate output. Changes to <property> trigger a re-render of your component with new values.
With a function, the wire service provisions results to the wired function via an object with either an error or data property. Check
for the presence of each in your JavaScript function, and set an appropriate property of your component. This triggers a re-render of
your component with new values.
Example code for each of these approaches is available in Wire Apex Methods to Lightning Web Components in the Lightning Web
Components Developer Guide.
handleLoad() {
getContactList()
.then(result => {
this.contacts = result;
})
.catch(error => {
this.error = error;
});
}
}
Offline-savvy components expect to be offline at times, and know what to do when that happens.
25
Use Salesforce Features While Offline Additional Documentation for Apex in Lightning Web
Components
refreshApex() is used to request a refresh for data provisioned by an Apex @wire. It’s usable today and, while online, behaves
as expected. However, while refreshApex(valueProvisionedByApexWireService) can be called while offline, it
requires a network connection to actually succeed, and push a new value to the wired property or function.
Using getRecordNotifyChange() and refreshApex() to request updates for cached data that are possibly stale is
described in Client-Side Caching of Apex Method Results in the Lightning Web Components Developer Guide.
Note: Support for offline images requires that both your Salesforce org and your mobile app are updated to the Spring ’23 release
or later.
IN THIS SECTION:
Image Priming and Offline Considerations
LWC Offline isn’t magic. If an image hasn’t been primed before you go offline, it can’t be displayed while offline. LWC Offline primes
image assets that are referenced in component template files, in the src attribute of a standard HTML img tag.
Use Images Uploaded as Files (ContentDocument) in an LWC
Files are a general mechanism to upload and make binary files, such as images, available in your Salesforce org. Files can be associated
with a specific record, which makes them ideal for product photos, images captured during a service call or other transaction, and
otherwise adding images to business activities that you track in Salesforce.
Use Images Uploaded as Asset Files
Asset files are the modern alternative to static resources. Asset files are ideal for images that are used throughout your components
and apps—for example, user interface elements like icons—or otherwise aren’t related to a specific record.
26
Use Salesforce Features While Offline Use Images Uploaded as Files (ContentDocument) in an LWC
Note: Only the img tag is supported at this time. Images referenced in other ways or tags aren’t primed and won’t display
sometimes, even when online.
Primed images are stored locally on the mobile device, in a new binary durable store (cache). Images stored in individual files are primed
and cached individually. Images stored in archive asset files are accessible, but be aware that the entire archive is primed and cached.
Updates to images in archive files require reloading the entire archive.
In Spring ’23, primed images aren’t purged from the offline cache when they get stale. Be mindful of the size of your images or archive
asset files, and the space they take up locally on devices.
The binary contents of an item referenced in an img tag isn’t validated. It’s up to you to ensure that referenced files are valid images.
Support for specific image formats is dependent on the capabilities of the web view, which is provided by the operating system. Providing
an unsupported or non-image file to an img tag is an HTML error and can cause unpredictable behavior. As the joke goes, don’t do
that. Safe formats are the usual web image formats: GIF, JPEG, PNG, and so on. When in doubt, test specific image formats on your specific
supported mobile devices and operating system versions.
get imageUrl() {
return getFieldValue(this.contentDocImage.data, IMAGE_URL_FIELD);
}
get resizedImageUrl() {
return getFieldValue(this.contentDocImage.data, IMAGE_URL_FIELD) +
'?thumb=THUMB240BY180';
27
Use Salesforce Features While Offline Use Images Uploaded as Files (ContentDocument) in an LWC
}
}
With the image URL provided by the getter functions in the preceding component JavaScript, referencing the images in the HTML
template is just like referencing any image in HTML. Use it in the src attribute of an HTML img tag.
<!-- imageFromContentDocument.html -->
<template>
<lightning-card>Display an image from
ContentDocument {imageUrl}</lightning-card>
<template if:true={contentDocImage}>
<img src={imageUrl}/>
</template>
<template if:true={contentDocImage}>
<img src={resizedImageUrl}/>
</template>
</template>
This feature works in LWC Offline beginning in Spring ’23, which is API version 57.0. Be sure to set that minimum API version for any
component that references images while offline.
<?xml version="1.0" encoding="UTF-8"?>
<!-- imageFromContentDocument-meta.xml -->
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__RecordAction</target>
<target>lightning__GlobalAction</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordAction,lightning__GlobalAction">
<actionType>ScreenAction</actionType>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
SEE ALSO:
User Interface API Developer Guide: Upload Files
Salesforce Object Reference Guide: ContentDocument
Salesforce Object Reference Guide: ContentVersion
28
Use Salesforce Features While Offline Use Images Uploaded as Asset Files
get assetArchiveUrl() {
return ARCHIVE_IMG + '&pathinarchive=images/logo-large.png';
}
}
Warning: The assetArchiveUrl function in the preceding example appends a pathinarchive query parameter and
value, using a “&” separator. The “&” isn’t used in examples in the standard LWC documentation. The need for the “&” separator is
inconsistent between desktop and mobile today, and we consider this discrepancy to be a software defect. For now, adding the
“&” separator generally works on both desktop and mobile, even though it results in a double “&&” on desktop.
With the image URL provided by the getter functions in the preceding component JavaScript, referencing the images in the HTML
template is just like referencing any image in HTML. Use it in the src attribute of an HTML img tag.
<!-- imageFromAssetFile.html -->
<template>
<lightning-card>
Display an image directly from an Asset file {assetUrl}
</lightning-card>
<template if:true={assetUrl}>
<img src={assetUrl}/>
</template>
<lightning-card>
Display an image from an archive Asset file {archUrl}
</lightning-card>
<template if:true={assetArchiveUrl}>
<img src={assetArchiveUrl}/>
</template>
29
Use Salesforce Features While Offline Navigation
</template>
SEE ALSO:
Salesforce Help: Asset Files
Lightning Web Components Developer Guide: Access Content Asset Files
Lightning Web Components Developer Guide: @salesforce/contentAssetUrlin @salesforce Modules
Navigation
Build navigation for Lightning web components.
When we talk about “navigation,” we can broadly separate things into two categories: things that appear on the screen—buttons, tabs,
links, and so on—and what actually happens when someone interacts with those things. Call these two categories navigation user
experience and navigation actions. These terms are loose, not specific to Salesforce, mobile apps, or Lightning web components, and we
use them informally here.
IN THIS SECTION:
Navigation User Experience
There are a variety of base components available to design the visual user interface that users tap or click to move around in your
app.
Base Components with Built-In Navigation Actions
Use base components that have automatic or built-in navigation actions.
Programmatic Navigation Actions
Some features require more complicated navigation designs. For the most complete control of your user interface and navigation
scheme, define navigation actions using JavaScript.
lightning-carousel-image Behaves like a hyperlink if a URL is provided via the href attribute.
30
Use Salesforce Features While Offline Programmatic Navigation Actions
lightning-formatted-address Displays a formatted address with a link to the given location on Google Maps.
lightning-formatted-phone Displays a phone number as a hyperlink with the tel: URL scheme.
lightning-formatted-rich-text Creates hyperlinks in rich text automatically for linkable text and email addresses.
lightning-formatted-text URLs and email addresses are displayed as hyperlinks when you specify the
linkify attribute.
lightning-pill Behaves like a hyperlink if a URL is provided via the href attribute.
lightning-vertical-navigation Behaves like a hyperlink if a URL is provided via the href attribute.
Note: Some of these components have limitations when used in an offline-enabled mobile app. See Base Components Support
for details.
There are additional base components that can have navigation actions attached to them, such as buttons and tabs. These components
require you to add click handlers or other functionality in the form of JavaScript code. See Programmatic Navigation Actions.
31
Use Salesforce Features While Offline Programmatic Navigation Actions
There’s a wide range of targets available for deep linking into the Field Service mobile app. See Deep Linking Schema for the Field
Service Mobile App in the Field Service Developer Guide for available URL formats.
Navigate to a Quick Action via Deep Link
Create a navigation action that opens a quick action, including quick actions built with LWCs.
{
"type": "standard__webPage",
"attributes": {
"url":
`com.salesforce.fieldservice://v1/sObject/${this.recordId}/quickaction/<api_name>`
}
}
This action can also be used as a deep link into the Field Service mobile app. See Deep Linking Schema for the Field Service Mobile
App in the Field Service Developer Guide for additional details.
Open Salesforce Mobile App via Deep Link
Create a navigation action that leaves the Field Service mobile app, and opens a specific page in the Salesforce mobile app.
{
"type": "standard__webPage",
"attributes": {
"url": "salesforce1://sObject/WorkOrder/home"
}
}
See Salesforce Mobile App URL Schemes for available URL formats.
Open Web Page
Create a navigation action that opens a screen that displays an external web page.
{
"type": "standard__webPage",
"attributes": {
"url": "https://salesforce.com"
}
}
32
Use Salesforce Features While Offline Programmatic Navigation Actions
33
CHAPTER 5 Development Tools and Processes
In this chapter ... Set up and use your development tools for the most efficient developer experience while building mobile
and offline LWCs.
• Set Up Your
Development
Developing LWCs and apps for use in Salesforce mobile apps uses all the same developer tools and
Environment processes that developing LWCs for desktop use. It also poses special challenges, especially in the areas
of testing and debugging. This chapter describes a number of additional tools and processes designed
• Validate Lightning
specifically for developing mobile LWCs.
Web Components for
Offline Use
• Develop
Offline-Ready LWCs
with the LWC Offline
Test Harness
34
Development Tools and Processes Set Up Your Development Environment
Note: Don’t let Android Studio or Xcode intimidate you. You only need to use it to provision a device emulator (Android) or
simulator (iOS). You don’t need both Android Studio and Xcode. If you want both, great. If not, pick the one you’re most comfortable
with.
While you can eventually choose to replace or add tools to this list, start with these and add to them as you become more experienced.
The Develop a Lightning Web Component Quick Action quick start provides hands-on instructions for setting up your development
environment, and also for creating, deploying, and running your first LWC.
We recommend the following installation sequence. See the links for more detailed instructions than those in the tutorial.
• Install and configure Android Studio.
• Install and configure Xcode.
• Install the Salesforce CLI.
• Install the Mobile Extensions.
• Install VS Code.
• Install Salesforce Extensions for VS Code.
• Download the latest virtual device build for the mobile app.
– For the Salesforce mobile app, use the standard virtual device builds, as described in Preview Your Components in the Salesforce
Mobile App in the Lightning Web Components Developer Guide.
Note: Your org must be enabled for LWC Offline before you can see or use LWC Offline features.
– For the Field Service mobile app, the download link is posted in the Trailblazer Community.
• Install a virtual device build into a configured Android emulator or iOS simulator instance.
35
Development Tools and Processes Install the Komaci Static Analyzer
IN THIS SECTION:
Install the Komaci Static Analyzer
The Komaci static analyser is an ESLint plugin that you install using a package manager, such as NPM or Yarn.
Troubleshoot Installation Problems
The Komaci Static Analyzer is implemented as a plugin for ESLint, a well-known JavaScript validation tool. ESLint plugins can be
finicky in their installation and configuration, requiring that all pieces are perfectly aligned for success.
Validate Components During Development
To ensure your components can be used in offline environments, watch for and act on the recommendations of the static analyzer.
Static Analyzer Validation Rules
The static analyzer validates that various code constructs and references in Lightning web components, such as wire decorators and
server calls, support offline priming. The majority of the rules focus on determining whether all code dependencies, such as imports
and modules, can be resolved; or on correct usage of offline-compatible wire adapters.
Note: Use Terminal (or your command line tool of choice) to run all commands from the root directory of your Lightning web
components project.
1. Add the Komaci Static Analyzer plugin and its dependencies to the development dependencies of your project.
NPM
npm install --save-dev @salesforce/eslint-plugin-lwc-graph-analyzer
YARN
yarn add --dev @salesforce/eslint-plugin-lwc-graph-analyzer
YARN
yarn install
"extends": [
36
Development Tools and Processes Troubleshoot Installation Problems
"@salesforce/eslint-config-lwc/recommended",
"plugin:@salesforce/lwc-graph-analyzer/recommended"
],
"overrides": [
...
Common Issues
• Install into your project — For the validation package to function properly, you must install all dependent packages using the
commands in the installation instructions from the root directory of your project. For example, the error in the preceding Output
panel screenshot indicates a missing package. This is due to not installing a package required for the project.
• Check your .eslintrc.json file — If you misconfigure your .eslintrc.json, the ESLint Output panel might show an
error related to that file. Ensure that the lines you add to your .eslintrc.json look exactly like the example in the installation
instructions and fix any syntax errors.
• ESLint must be authorized — If you don’t see any feedback in the ESLint output view, the ESLint server itself is likely not getting
initialized for your project. Make sure that you have:
– Installed theESLint extension for Visual Studio Code.
– Installed project dependencies into your project with npm install (or yarn install) from your project’s root directory.
The command installs the ESLint package and other dependencies locally to your project.
– Authorized ESLint to run in your project. If it’s not, you might see an “error” squiggly line in the first line of your JavaScript file,
indicating that ESLint isn’t authorized. Hovering over that error gives you the option to authorize it for your project.
37
Development Tools and Processes Static Analyzer Validation Rules
To fix an issue, hover over the problematic code. A pop-up appears near the code, describing what the problem is and how to fix it.
• You’ll see a description of the issue, along with the linting package that found the issue.
• Click View Problem to highlight the code causing the issue, with a box underneath describing the issue and what linting package
found the issue.
• Click Quick Fix to show a dropdown of options of how to fix the issue. Then, select an option from the menu to automatically
perform that fix.
38
Development Tools and Processes Prerequisites & Setup Considerations
Test Harness helps you confirm that your LWCs work as expected in LWC Offline-based environments and are ready for integration
testing within an offline-enabled Salesforce mobile app.
Features
• Quick and convenient app flow, centered around launching LWC Quick Actions with a selected SObject.
• Dedicated draft queue, for viewing the status of pending data modification operations.
• Debug Console embedded into the app, for both a broad view of ongoing tasks and granular inspection of log messages.
• Immediate, on-demand app reloads for quickly re-bootstrapping and re-running your latest LWC code changes.
• Uses the latest version of LWC Offline.
• Can attach browser debuggers to view more developer-specific errors and warnings from the LWC webview.
IN THIS SECTION:
Prerequisites & Setup Considerations
To fully use Test Harness, you need a working mobile development environment. If you haven’t set up your mobile development
tools yet, see the following resources for guidance.
Download and Install — Android
After you’ve gotten your mobile development tools up and running, getting started with Test Harness on Android devices is a breeze.
Download and Install — iOS
After you’ve gotten your mobile development tools up and running, getting started with Test Harness on iOS devices is a breeze.
Use the Test Harness
Learn how to use the Test Harness to perform common testing and debugging actions.
Debug Lightning Web Components
The best way to develop and debug your Lightning web components is the same way you develop and debug anything built with
HTML, CSS, and JavaScript: with the built-in debugging tools in your web browser.
39
Development Tools and Processes Download and Install — Android
3. Using Finder (macOS) or File Explorer (Windows), navigate to your Downloads folder.
4. Find the Test Harness APK file you downloaded, and drag it onto a running Android emulator.
The Test Harness app is now installed on your emulated device!
IN THIS SECTION:
Create a Quick Action with an LWC
Display and Run an LWC from a Quick Action
40
Development Tools and Processes Debug Lightning Web Components
After a component is deployed to Salesforce, create a Quick Action that uses it. For details, see Quick Actions in the Lightning Web
Components Developer Guide.
Tip: You don’t need to add Quick Actions to a page layout to see it in Test Harness. The Test Harness app automatically shows all
LWC-based Quick Actions for the displayed sObject type.
Tip: If the list is empty, log in to the org with the same user and view a few sample records you wish to test with. Those records
are added to the Most Recently Viewed list in Salesforce. Return to Test Harness and access the sObject again by tapping on it.
The recently viewed records appear in the list.
When you tap a record, you’re presented with all of the Quick Actions that are defined in Buttons, Links, and Actions in Setup for the
selected sObject.
Note: To streamline the user interface and focus on development activities, Test Harness only displays Lightning web
component-based Quick Actions.
Tap a Quick Action to launch the LWC. If the LWC exposes a recordId property using the @api annotation, the LWC receives the
ID of the tapped record set on that property.
IN THIS SECTION:
Enable Debugging for LWC Developers
Admins must enable debugging for each LWC developer user that uses Test Harness in their org.
41
Development Tools and Processes Debug Lightning Web Components
Debug in Android
1. On your emulated Android device, open the Settings app.
2. Enter About emulated device into the search bar, and tap the result titled About emulated device.
3. Scroll to the bottom of the page and tap Build number seven times. The message “You are now a developer!” appears after the
seventh tap, meaning that developer mode is enabled for the emulator.
4. Launch Chrome on your desktop. In the location bar, enter chrome://inspect/#devices.
5. Select Inspect for the WebView under the Remote Target emulator you’re using.
A window appears containing your emulator with Chrome DevTools connected to it. You can use Chrome DevTools to inspect the
LWC element, set breakpoints, and see the console output. As with iOS, you can use the standard web development techniques for
working with HTML, CSS, and JavaScript to run, test, debug, and improve your LWC.
42
Development Tools and Processes Debug Lightning Web Components
For additional information on the basics of debugging with Chrome Developer Tools, see Remote debugging WebViews (Google).
For a deeper dive on debugging JavaScript in an embedded WebView of an Android mobile app, see Debugging embedded JavaScript
in an Android app using Chrome DevTools.
Debug in iOS
Note: Debugging in iOS currently only works with simulators running on macOS Big Sur or later, and requires using the Safari
Technology Preview browser.
1. Launch Safari on your Desktop.
2. Select Safari > Preferences.
3. Select Advanced.
43
Development Tools and Processes Debug Lightning Web Components
4. Enable Show Develop menu in menu bar. Then, close the Preferences panel.
5. Select Develop > Simulator - device - version, where the Simulator is the one that you’ve opened with the Test Harness app for
testing your new Lightning web component.
A window appears, showing the Safari Web Inspector developer tools connected to your simulator. You can use these tools and
standard web development techniques to refine and improve your content.
For additional information about the Web Inspector and how to use it, see Apple Web Development Tools (Apple).
44
CHAPTER 6 Quick Start Tutorials
In this chapter ... These hands-on tutorials get you started with creating custom LWCs for your mobile apps.
• Develop a Lightning
Web Component
Quick Action
• Debug Lightning Web
Components in the
Field Service Mobile
App
45
Quick Start Tutorials Develop a Lightning Web Component Quick Action
IN THIS SECTION:
Prerequisites
Ensure you’re ready for this tutorial by verifying that you have the right software installed and configured, and your org has Lightning
web components enabled for mobile users.
Field Service Org Setup
Create a permission set with the Access Lightning Web Components in Field Service Mobile permission,
and assign this permission set to users who develop for or use Lightning web components in the mobile app.
iOS Simulator Setup
During development it’s convenient to test your code in a virtual device. Use Xcode to create a device simulator, and install the
virtual device build of your mobile app into it.
Android Emulator Setup
During development it’s convenient to test your code in a virtual device. Use Android Studio to create a device emulator, and install
the virtual device build of your mobile app into it.
Workspace Setup
Set up your development environment, create a project to develop your LWC in, and connect your project to Salesforce.
Create and Configure a Lightning Web Component
In this section, you’ll create a basic component named demoLWCAction that displays a “Hello World!” message on the screen.
Prerequisites
Ensure you’re ready for this tutorial by verifying that you have the right software installed and configured, and your org has Lightning
web components enabled for mobile users.
• Salesforce CLI is installed and up-to-date.
• VS Code is installed and up-to-date.
• Salesforce Extension Pack for VS Code is installed and up-to-date.
Note: Ensure all prerequisites listed on the extension pack page are also satisfied.
Note: This step is required for Field Service orgs. If you’re using LWC Offline in the Salesforce mobile app, skip to the next step.
46
Quick Start Tutorials Field Service Org Setup
Lightning web components for LWC Offline-enabled mobile apps is an opt-in feature. To enable it for your org, you must create and
assign a permission set for your mobile users that includes enabling the Access Lightning Web Components in Field
Service Mobile permission. This permission set is required to access and run LWCs within the Field Service mobile app. (While
not technically required to develop LWCs, you’ll have a hard time developing components when you can’t run them.)
You can use this one permission set for any number of Lightning web components and mobile users.
IN THIS SECTION:
Define a Permission Set for Your Org
Create a permission set that applies the permissions required to enable Lightning web components to users with the permission
set.
Assign the Permission Set to a Mobile User
Assign the permission set that enables Lightning web components to users who must use or develop LWCs.
3. Click Save.
4. In the Find Settings box, enter Access Lightning Web Components in Field Service Mobile and click it.
5. Click Edit.
6. Select the Access Lightning Web Components in Field Service Mobile checkbox.
7. Click Save.
6. Click Assign.
47
Quick Start Tutorials iOS Simulator Setup
IN THIS SECTION:
Configure Minimum Required iOS Simulator Settings
Ensure your virtual device meets the minimum device and iOS version requirements to run your mobile app.
Install the Field Service App for iOS
To run the Field Service mobile app in an iOS simulator, download and install a virtual device build of the app. After it’s installed,
open the app and log into your development org.
After the device simulator launches you can close Xcode, but keep the Simulator app’s window open to install the Field Service app in
the next section.
5. Click (simulator tap, you get the idea) the Field Service app. Click Get Started.
6. Click the screen to run through the tutorial or click Skip.
48
Quick Start Tutorials Android Emulator Setup
Warning: If you’re logging in as a community user for the first time, click to add a new connection.
• For Host, enter your org’s URL in the following format: https://[yourURL].my.salesforce.com.
• For Label, enter a nickname for your connection.
IN THIS SECTION:
Configure Minimum Required Android Emulator Settings
Ensure your virtual device meets the minimum device and Android API version requirements to run your mobile app.
Install the Field Service App for Android
To run the Field Service mobile app in an Android emulator, download and install a virtual device build of the app. After it’s installed,
open the app and log into your development org.
49
Quick Start Tutorials Android Emulator Setup
50
Quick Start Tutorials Android Emulator Setup
2. Drag the downloaded .apk file into the Android Emulator window.
3. Click a blank space on the Android Emulator’s screen and drag up to view the installed apps.
4. Click the newly installed Field Service app.
5. Click I Agree to accept the Order Form Supplement agreement.
51
Quick Start Tutorials Workspace Setup
6. Click the vertical dots button on the top right and select Change Server.
7. Select a connection.
Warning: If you’re logging in as a community user for the first time, click Add New Connection, fill in the form as indicated
in the following bullet list, and then click Apply to save the changes.
• For Name, enter a nickname for your connection.
• For URL, enter your org’s URL. It must be in the following format: https://[yourURL].my.salesforce.com
8. Click the arrow in the top left to go back to the login screen.
9. Enter your username and password for your org.
10. Click Log In.
11. Click Allow to allow the app to access your Salesforce information.
12. Click through the various permissions screens and allow them the appropriate access.
When finished, you arrive at the app’s home screen.
Workspace Setup
Set up your development environment, create a project to develop your LWC in, and connect your project to Salesforce.
1. Launch VS Code.
2. Select File > Open and open a project to use with your org.
Note: If you don’t have a project created, you can open the VS Code Command Palette by clicking View > Command Palette,
enter SFDX, and select SFDX: Create a Project. Then follow the prompts.
3. In the menu bar, select View > Terminal to open VS Code’s integrated terminal if it’s not already visible.
52
Quick Start Tutorials Create and Configure a Lightning Web Component
4. Run the following command to install the Salesforce Extension Plugins for mobile.
sfdx plugins:install @salesforce/lwc-dev-mobile
5. In the menu bar, select View > Command Palette to open the VS Code Command Palette.
6. Enter SFDX and select SFDX: Authorize an Org.
7. Select the org you want to use and press Enter.
A web page opens for the org login.
8. Verify your org authorization by checking the bottom left in VS Code.
You should see your username in the VS Code status bar.
If you have difficulty authorizing SFDX for access to your org, see SFDX Authorization.
IN THIS SECTION:
Create and Deploy a Lightning Web Component to Salesforce
Create a simple Lightning web component, configure its metadata, and then deploy the component to your Salesforce org—all
from within VS Code.
Verify the Component Was Deployed to Your Org
The simplest way to verify that a Lightning web component is available in your org is to view the list of components in Setup in
Salesforce.
Create a Quick Action in Salesforce
To access your Lightning web component, you must assign your component to a new Quick Action, and then assign the Quick
Action to a page layout in Salesforce. This configuration makes it visible in the Actions launcher in the mobile app.
Add the Quick Action to a Page Layout in Salesforce
Make a Lightning web component quick action available in the mobile app by adding it to the mobile actions section of a page
layout.
Clear Cached Metadata
To see changes to a Lightning web component as you develop, cached metadata must be cleared in the mobile app. Clear cached
metadata every time new code is deployed to the org to see your changes.
Run the Quick Action in the Mobile App
Let’s finally see that Lightning web component working in the Field Service mobile app.
53
Quick Start Tutorials Create and Configure a Lightning Web Component
4. In the newly created demoLWCAction.js-meta.xml, replace the default code with the following and save the file:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>56.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordAction</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordAction">
<actionType>ScreenAction</actionType>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
5. In the VS Code explorer, right-click the force-app/main/default/lwc/demoLWCAction folder and select SFDX:
Deploy Source to Org.
54
Quick Start Tutorials Create and Configure a Lightning Web Component
55
Quick Start Tutorials Create and Configure a Lightning Web Component
5. Click Save.
6. Click Service Appointment next to Object Name to return to the Service Appointment object page.
Note: Mobile quick actions are only added to the mobile layout. You can’t verify that mobile quick actions were successfully
added to the correct page layout using the desktop experience.
56
Quick Start Tutorials Create and Configure a Lightning Web Component
Android
57
Quick Start Tutorials Create and Configure a Lightning Web Component
iOS
Android
58
Quick Start Tutorials Debug Lightning Web Components in the Field Service Mobile
App
iOS
IN THIS SECTION:
Install Local Development Server Plugin
Whether you’re debugging your component for iOS or Android, you must first install the LWC Development Server for mobile.
Debug in iOS
Connect Safari Web Inspector on your desktop to the WebView in the Field Service mobile app where your LWC is running.
Debug in Android
Connect Chrome DevTools on your desktop to the WebView in the Field Service mobile app where your LWC is running.
59
Quick Start Tutorials Debug in iOS
Note: If you encounter an error when updating Salesforce CLI, see Update Salesforce CLI for troubleshooting instructions.
2. In the same terminal window, run the following command to install the LWC Development Server for mobile.
sfdx plugins:install @salesforce/lwc-dev-server
Debug in iOS
Connect Safari Web Inspector on your desktop to the WebView in the Field Service mobile app where your LWC is running.
Note: Debugging in iOS currently only works with Big Sur or later, and requires using the Safari Technology Preview browser.
See Apple Web Development Tools for additional information about the Web Inspector and how to use it.
60
Quick Start Tutorials Debug in Android
Debug in Android
Connect Chrome DevTools on your desktop to the WebView in the Field Service mobile app where your LWC is running.
1. On your emulated Android device, open the Settings app.
2. Enter About emulated device into the search bar and click it.
3. Scroll to the bottom of the page and click Build number seven times.
The message “You are now a developer!” appears when you click it enough, indicating that developer mode is enabled for the
emulator.
4. Launch Chrome on your desktop.
5. In the location bar, enter chrome://inspect/#devices.
6. Click Inspect for the WebView under the Remote Target emulator you’re using.
A window appears containing your emulator with Chrome DevTools connected to it. You can use Chrome DevTools to inspect the LWC
element, set breakpoints, and see the console output. As with iOS, you can use the standard web development techniques for working
with HTML, JavaScript, and CSS to run, test, debug, and improve your LWC.
61
Quick Start Tutorials Debug in Android
See Remote debugging WebViews for additional basics, and Debugging embedded JavaScript in an Android app using Chrome DevTools
for a deep dive on debugging JavaScript in an embedded WebView of an Android mobile app.
62