0

I have a custom LWC widget in Account record page, when page loaded my widget, I need to know the account's ID or record id(e.g. "0015g00000Bkgg6AAB"), my testing code below:

import { LightningElement, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

const fields = [NAME_FIELD, REVENUE_FIELD, BillingAddress_FIELD];
export default class AccountCreator extends LightningElement {
  @wire(getRecord, { recordId: '$recordId', fields })
  account;
  get name() {
    return getFieldValue(this.account.data,NAME_FIELD);
  }
  get revenue() {
    return getFieldValue(this.account.data, REVENUE_FIELD);
  }
  get address() {
    return getFieldValue(this.account.data, BillingAddress_FIELD);
  }
}
JamesFdl
  • 15
  • 1
  • 4

1 Answers1

0

Add these two lines in your class

import { LightningElement, wire, api } from 'lwc';   // 1 - add "api"
export default class AccountCreator extends LightningElement {
    @api recordId; // 2 - add variable to magically store the current record's id
}

There are 2 special variables for learning current record's id and current sObject type (if you want to build component that behaves differently when dropped on different pages). Some more info here: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_record_context

Your code looks like you copied from an example, your @wire already listens to changes of the recordId - you just didn't have this variable defined.

eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Hi man, I got this question resolved but a new problem is I can get billingcountry in this way `import BillingCountry_FIELD from '@salesforce/schema/Account.BillingCountry';` but can not get billingcountrycode in the same way `import BillingCountryCode_FIELD from '@salesforce/schema/Account.BillingCountryCode';` I got an error message "Invalid reference Account.BillingCountryCode of type sobjectField in file accountCreator.js" when I push code to SF do you know why? – JamesFdl Apr 13 '21 at 09:16
  • Do you have "state and country picklists" enabled in this org? – eyescream Apr 13 '21 at 10:04
  • 1
    Just enabled "state and country picklists" in my org, it works now, thank you very much. – JamesFdl Apr 14 '21 at 07:23
  • Hi man, I did it as you suggested to turn on "State and Country/Territory Picklists" a few days ago and it worked, but today it doesn't work, I go to SetUp to check it again, the "State and Country/Territory Picklists" is still enabled, do you know why? – JamesFdl Apr 20 '21 at 07:51
  • You're still connecting to right org (it's not a scratch org that expired for example?). Same compilation error? – eyescream Apr 20 '21 at 07:54
  • Yes, I did create a new scratch org, thx. BTW, another issue I use "Auth an dev hub" to auth my trailhead background and then use "create a default scratch org", and then I open the default org, the page is different, it is not my trailhead background, why? – JamesFdl Apr 20 '21 at 10:12
  • Scratch orgs by default are empty, like if you'd sign up for new Developer Edition. You control the features you need in the `project-scratch-def.json` file (support for communities, some settings enabled, sample data) but that's it. If you need your code and objects - you need to deploy. That includes apps, tabs from trailhead org etc. If you need dependency to another managed package - you deploy or you edit `sfdx-project.json` (I think). It's not an exact copy like sandbox. But read about "org shapes" – eyescream Apr 20 '21 at 12:07