2

I'm building a Lightning Web Component that loads several records, modifies them based on user input then writes them back to to the database.

It appears there is no way to instantiate object records in LWC as you can do in APEX.

LWC documentation does mention the ability to import references to objects, but they do not appear to be classes.

For example, the following code:

import { LightningElement } from "lwc";
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class CheckIn extends LightningElement {

    myAccount = new ACCOUNT_OBJECT();
}

Results in this error:

[n is not a constructor]

2

2 Answers 2

3

You said the LWC would load records so that's what you have to do.

Details can be found here

https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_wire_adapters_record

but basically you just have to use the getRecord wire method

import { getRecord} from 'lightning/uiRecordApi';

The full example by the documentation:

import { LightningElement, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';
import PHONE_FIELD from '@salesforce/schema/Account.Phone';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';

export default class Example extends LightningElement {
    @wire(getRecord, { recordId: '001456789012345678', fields: [NAME_FIELD, INDUSTRY_FIELD], optionalFields: [PHONE_FIELD, OWNER_NAME_FIELD] })
    account;

     get name() {
        return getFieldValue(this.account.data, NAME_FIELD);
    }

    get phone() {
        return getFieldValue(this.account.data, PHONE_FIELD);
    }

    get industry(){
        return getFieldValue(this.account.data, INDUSTRY_FIELD);
    }

    get owner() {
        return getFieldValue(this.account.data, OWNER_NAME_FIELD);
    }
}

To update it you'll need to call the updateRecord method with the properly formatted parameters

updateRecord(recordInput: Record, clientOptions?: Object): Promise<Record>

where recordInput is

{
 "apiName": "Account",
 "fields": {
   "Name": "Universal Containers"
 }
}
1

JavaScript isn't a strongly typed language, and classes are a recent addition to the mix. When it comes to instantiating objects, you'll be working with a vanilla JavaScript Object. The schema of that Object does matter -- you need to know where you're going to eventually consume it.

If you're going to pass your new Account to the uiRecordApi's updateRecord method, use the pattern Krysia suggests:

let recordInput = {
   apiName: 'Account',
   fields: {
       Name: 'Universal Containers'
   }
}

uiRecordApi isn't suitable for a lot of use cases, in which case you'll want to use Apex. If your @AuraEnabled Apex method takes an SObject as a parameter like this...

@AuraEnabled
public static Account saveAccount(Account acct) {
    ....
}

...pass in a JavaScript Object that looks like this:

let accountRecord = {
   Name: 'Universal Containers'
}

Apex is able to interpret this as an Account.

2
  • 1
    Can you point me to the documentation that talks to your last code block? ( in regard to passing in JS Objects with the 'sbojectType')
    – doz87
    Jan 19, 2020 at 3:38
  • 2
    Thanks for calling my bluff on that. I tested, and that sobjectType property turned out to be an apocryphal requirement. Passing in SObjects works fine with or without it. I have edited my post. Jan 20, 2020 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.