Develop Secure Code
HTML Template Directives
HTML Template Errors
Decorators
@salesforce Modules
PageReference Types
@salesforce ModulesModules scoped with @salesforce add functionality to Lightning web components at runtime.
Modules that are imported without @salesforce, like lightning/uiRecordApi, contain resources that don’t change and are universal to all orgs.
Some modules have a fixed set of module identifiers. For example, the @salesforce/i18n module supports many identifiers including @salesforce/i18n/dir and @salesforce/i18n/lang.
Some modules have a dynamic set of module identifiers. The set is defined by the organization’s metadata. For example, @salesforce/schema accesses an organization’s objects and fields.
Import Apex methods from @salesforce/apex. The imported methods are functions that the component can call either via @wire or imperatively. See Call Apex Methods.
1import apexMethodName from '@salesforce/apex/Namespace.Classname.apexMethodReference';
2@wire(apexMethodName, { apexMethodParams })
3propertyOrFunction;apexMethodName—A symbol that identifies the Apex method.apexMethodReference—The name of the Apex method to import.Classname—The name of the Apex class.Namespace—The namespace of the Salesforce organization. Specify a namespace unless the organization uses the default namespace (c), in which case don’t specify it.apexMethodParams—An object with properties that match the parameters of the apexMethod, if needed. If a parameter value is null, the method is called. If a parameter value is undefined, the method isn’t called. If the Apex method is overloaded, the choice of what method to call is non-deterministic (effectively random), and the parameters passed may cause errors now or in the future. Don’t overload @AuraEnabled Apex methods.apexMethodParams is an object. To pass parameter values to an Apex method, pass an object whose properties match the parameters of the Apex method. For example, if the Apex method takes a string parameter, don’t pass a string directly. Instead, pass an object that contains a property whose value is a string.
Important
Read the data that’s returned by the wire adapter using a property or function.
propertyOrFunction—A private property or function that receives the stream of data from the wire service.
@wire, the results are returned to the property’s data property or error property.@wire, the results are returned in an object with a data property and an error property.The data property and the error property are hardcoded values in the API. You must use these values.
Note
1// Example
2import getAccounts from '@salesforce/apex/MyAccountController.getAccounts';
3
4// Use the wire service to provision data to either a property or a function.
5// This example provisions data to a property.
6@wire(getAccounts)
7accounts;
8
9// This example provisions data to a function.
10@wire(getAccounts)
11wiredAccounts({ error, data }) {
12 if (data) {
13 this.accounts = data;
14 this.error = undefined;
15 } else if (error) {
16 this.error = error;
17 this.accounts = undefined;
18 }
19}Import getSObjectValue from @salesforce/apex. Call this method to get a field value from an object retrieved from an Apex method.
1import { getSObjectValue } from "@salesforce/apex";
2getSObjectValue(sObject, fieldApiName);sObject—The object returned by the Apex method.
fieldApiName—The API name of the field. The value can be either a string or reference to a field imported from @salesforce/schema. You can specify up to three relationship fields to reference parent objects and fields using this format: <SObjectName>.<relationship-1>.<relationship-2>.<relationship-3>.<fieldName>.
Import refreshApex from @salesforce/apex. Call this method to query the server for updated data returned by Apex, and refresh the cache of a wired property.
1import { refreshApex } from "@salesforce/apex";
2refreshApex(valueProvisionedByApexWireService);The use of refreshApex to refresh data from non-Apex wire adapters is deprecated. To refresh record data returned by a non-Apex wire adapter, use notifyRecordUpdateAvailable(recordIds) instead.
Note
{valueProvisionedByApexWireService} is either a property annotated with an Apex @wire, or if you annotated a function, the argument that the wired function receives. See Refresh the Cache When Using @wire.To update a record via imperative Apex, call the Apex method and then call notifyRecordUpdateAvailable() to update the cache. The refreshApex() function doesn’t refresh data that was fetched by calling an Apex method imperatively.
Note
Import Apex methods from @salesforce/apexContinuation.
1// Syntax
2import apexMethodName from "@salesforce/apexContinuation/Namespace.Classname.apexMethodName";1// Example
2import startRequest from "@salesforce/apexContinuation/SampleContinuationClass.startRequest";apexMethodName—A symbol that identifies the Apex method.apexMethodReference—The name of the Apex method to import.Classname—The name of the Apex class.Namespace—If the class is in the same namespace as the component, don’t specify a namespace. If the class is in a managed package, specify the namespace of the managed package.See Make Long-Running Callouts with Continuations.
1// Syntax
2import formFactorPropertyName from @salesforce/client/formFactor1// Example
2import CLIENT_FORM_FACTOR from @salesforce/client/formFactorformFactorPropertyName—A name that refers to the form factor of the hardware running the browser. Possible values are:
Large—A desktop client.Medium—A tablet client.Small—A phone client.Pass the form factor to the getRecordCreateDefaults wire adapter to get the default layout information and object information for creating a record.
Import the network ID of the current Experience Builder site from @salesforce/community/Id.
1// Syntax
2import idName from "@salesforce/community/Id";1// Example
2import networkId from "@salesforce/community/Id";Import the base URL of your Experience Builder site from @salesforce/community/basePath.
1// Syntax
2import basepathname from "@salesforce/community/basePath";1// Example
2import sitePath from "@salesforce/community/basePath";The base path is the section of the site’s URL that comes after the domain. So if your site domain name is UniversalTelco.force.com and myPartnerSite was the URL value added when you created the site, the community’s URL is UniversalTelco.force.com/myPartnerSite/s. In this case, myPartnerSite/s is the base path.
See Get Information About the Current Experience Builder Site.
If a component imports @salesforce/community, it can target only an Experience Builder page. The component can’t be used in any other Salesforce container. See Configure a Component for Experience Builder.
Important
Import content asset files from @salesforce/contentAssetUrl.
1// Syntax
2import myContentAsset from "@salesforce/contentAssetUrl/contentAssetReference";1// Syntax for assets in managed packages
2import myContentAsset from "@salesforce/contentAssetUrl/namespace__contentAssetReference";1// Example
2import SALES_WAVE_LOGO from "@salesforce/contentAssetUrl/SalesWaveLogo";myContentAsset—A name that refers to the asset file.
contentAssetReference—The name of the asset file.
An asset file name can contain only underscores and alphanumeric characters, and must be unique in your org. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.
namespace—If the asset file is in a managed package, this value is the namespace of the managed package.
See Access Content Asset Files.
Import internationalization properties from @salesforce/i18n.
1// Syntax
2import internationalizationPropertyName from @salesforce/i18n/internationalizationProperty1// Example
2import LANG from "@salesforce/i18n/lang";internationalizationPropertyName—A symbol that identifies the internationalization property.
internationalizationProperty—An internationalization property.
The internationalization properties are listed in Access Internationalization Properties.
Import labels in your Salesforce organization from @salesforce/label.
1// Syntax
2import labelName from "@salesforce/label/labelReference";1// Example
2import greeting from "@salesforce/label/c.greeting";labelName—A name that refers to the label.labelReference—The name of the label in your org in the format namespace.labelName. We use this format because it’s the same format used in managed packages, in Visualforce, and in other Salesforce technologies. You can use the same format to access labels, myns.labelName, regardless of where you’re accessing them.See Access Labels.
Import a Lightning message channel that a component can use to communicate via the Lightning Message Service.
1// Syntax
2import channelName from "@salesforce/messageChannel/channelReference";1// Syntax for resources in a managed package
2import channelName from "@salesforce/messageChannel/channelName__channelReference";1// Example
2import SAMPLEMC from "@salesforce/messageChannel/SampleMessageChannel__c";channelName—A symbol that identifies the message channel.
channelReference—The API name of the message channel.
namespace—If the message channel is in a managed package, this value is the namespace of the managed package. If the message channel is not in a managed package, do not include a namespace.
Import static resources in your Salesforce organization from @salesforce/resourceUrl.
1// Syntax
2import resourceName from "@salesforce/resourceUrl/resourceReference";1// Syntax for resources in a managed package
2import myResource from "@salesforce/resourceUrl/namespace__resourceReference";1// Example
2import TRAILHEAD_LOGO from "@salesforce/resourceUrl/trailhead_logo";myResource—A name that refers to the static resource.
resourceReference—The name of the static resource.
A static resource name can contain only underscores and alphanumeric characters, and must be unique in your org. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores.
namespace—If the static resource is in a managed package, this value is the namespace of the managed package.
Import references to Salesforce objects and fields from @salesforce/schema.
1// Syntax
2import objectName from "@salesforce/schema/objectReference";
3import objectName from "@salesforce/schema/namespace__objectReference";
4import fieldName from "@salesforce/schema/object.fieldReference";1// Example
2import POSITION_OBJECT from "@salesforce/schema/Position__c";
3import POSITION_OBJECT from "@salesforce/schema/ns__Position__c";
4import NAME_FIELD from "@salesforce/schema/Account.Name";
5import SPANNING_FIELD from "@salesforce/schema/Account.Owner.Name";objectName—A symbol that identifies the Salesforce object.objectReference—The name of a Salesforce object.fieldName—An imported symbol that identifies the Salesforce field.fieldReference—The name of a Salesforce field. You can specify up to three relationship fields to reference parent objects and fields using this format: <SObjectName>.<relationship-1>.<relationship-2>.<relationship-3>.<fieldName>.namespace—If the object or field is in a managed package, this value is the namespace of the managed package.See Import References to Salesforce Objects and Fields.
Import the site ID of the current Experience Builder site from @salesforce/site/Id.
1// Syntax
2import idName from "@salesforce/site/Id";1// Example
2import siteId from "@salesforce/site/Id";Import the list of active languages in your Experience Builder site from @salesforce/site/activeLanguages.
1// Syntax
2import LANGUAGES from "@salesforce/site/activeLanguages";1import siteLanguages from "@salesforce/site/activeLanguages";The list of active languages in your Experience Builder site includes metadata on the default site language and all other active languages. You can configure a site’s languages in Experience Builder in Settings | Languages. Inactive languages are excluded from the list. The return value is an array of language objects, where each object includes the language’s label and the code, such as en-US. The array is sorted alphabetically by the label.
See Get Information About the Current Experience Builder Site.
If a component imports @salesforce/community or @salesforce/site, it can target only an Experience Builder page. You can’t use the component in any other Salesforce container. See Configure a Component for Experience Builder.
Important
Import the current user’s ID from @salesforce/user/Id.
1// Syntax
2import userId from "@salesforce/user/Id";1// Example
2import Id from "@salesforce/user/Id";userId—An imported symbol that identifies the Salesforce user ID.
To determine whether the user is a guest user, import the current user’s status from @salesforce/user/isGuest.
1// Syntax
2import isGuestUser from "@salesforce/user/isGuest";1// Example
2import isGuest from "@salesforce/user/isGuest";isGuestUser—A symbol that identifies whether the user is a guest user (true) or not (false).
See Get Information About the Current User.
Import a permission and check whether it’s assigned to the current user.
1// Syntax
2import hasPermission from "@salesforce/userPermission/Permission";1// Example
2import { LightningElement } from 'lwc';
3import hasViewSetup from '@salesforce/userPermission/ViewSetup';
4
5export default class App extends LightingElement {
6 get isSetupEnabled() {
7 return hasViewSetup;
8 }
9
10 openSetup(e) {...}
11}hasPermission—A boolean indicating whether the current user has the imported permission (true) or not (undefined).
Permission—The name of a Salesforce user permission.
See Check Permissions.
Import a custom permission and check whether it’s assigned to the current user.
1// Syntax
2import hasPermission from '@salesforce/customPermission/Permission';
3import hasPermission from '@salesforce/customPermission/namespace__Permission;1// Example
2import { LightningElement } from "lwc";
3import hasViewReport from "@salesforce/customPermission/acme__ViewReport";
4
5export default class App extends LightingElement {
6 get isReportVisible() {
7 return hasViewReport;
8 }
9}hasPermission—A boolean indicating whether the current user has the imported permission (true) or not (undefined).
Permission—The name of a Salesforce custom permission.
See Check Permissions.