Newer Version Available

This content describes an older version of this product. View Latest

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.

Treat errors as a normal, expected outcome, rather than a failure condition. Provide appropriate feedback to the user, and suggest alternative behavior, rather than treating the situation as unexpected.

1// apexImperativeMethod.js
2import { LightningElement, track } from 'lwc';
3import getContactList from '@salesforce/apex/ContactController.getContactList';
4
5export default class ApexImperativeMethod extends LightningElement {
6    @track contacts;
7    @track error;
8
9    handleLoad() {
10        getContactList()
11            .then(result => {
12                this.contacts = result;
13            })
14            .catch(error => {
15                this.error = error;
16            });
17    }
18}

Offline-savvy components expect to be offline at times, and know what to do when that happens.