In the salesforce trailhead "Lightning Web Components and Salesforce Data: Use Apex to Work with Data" we have the following code:
import { LightningElement, wire } from "lwc";
import NAME_FIELD from "@salesforce/schema/Account.Name";
import REVENUE_FIELD from "@salesforce/schema/Account.AnnualRevenue";
import INDUSTRY_FIELD from "@salesforce/schema/Account.Industry";
import getAccounts from "@salesforce/apex/AccountController.getAccounts";
const COLUMNS = [
{ label: "Account Name", fieldName: NAME_FIELD.fieldApiName, type: "text" },
{
label: "Annual Revenue",
fieldName: REVENUE_FIELD.fieldApiName,
type: "currency"
},
{ label: "Industry", fieldName: INDUSTRY_FIELD.fieldApiName, type: "text" }
];
export default class AccountList extends LightningElement {
columns = COLUMNS;
@wire(getAccounts)
accounts;
}
In this example, and in many others, field variables such as FIRST_NAME are imported via the "@salesforce/schema/
methodology. Later, they are referenced via code such as REVENUE_FIELD.fieldApiName
.
Why is this? Is there a technical reason for it? Is it just a syntax standard? Given that the api name is already hardcoded via the import
lines, why not just move that coding directly to where the API names are referenced? Something like this:
const COLUMNS = [
{ label: "Account Name", fieldName: 'Name', type: "text" },
{ label: "Annual Revenue", fieldName: "AnnualRevenue", type: "currency" },
{ label: "Industry", fieldName: "Industry", type: "text" }
];