4

I am using getRecordCreateDefaults and it has an optional recordTypeId property in its argument object, defined as:

(Optional)The ID of the record type (RecordType object) for the new record. If not provided, the default record type is used.

import { LightningElement, api, wire, track } from 'lwc';
import { getRecordCreateDefaults } from 'lightning/uiRecordApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';

export default class DynamicForm extends LightningElement

    @api selectedRecordTypeId = null;

    @wire(getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '$selectedRecordTypeId' })
    accountCreateDefaults;

    connectedCallback() {
        const data = this.accountCreateDefaults.data;
        // process it
    }
}

Sometimes selectedRecordTypeId will be supplied and other times it will not, and will be null. If selectedRecordTypeId is null the getRecordCreateDefaults returns an empty object. That is, this.accountCreateDefaults is an empty object. If selectedRecordTypeId is a valid record type ID, it all works fine.

The recordTypeId property is optional in the object argument, so if I don't include it at all then it also works, returning the desired, default data.

However, there does not seem to be a way to sometimes specify recordTypeId as a property in the wired object parameter, and sometimes not.

I thought I found a workaround by calling the method imperatively, but that is forbidden by the framework and results in a run-time error of "Imperative use is not supported". Of course, I could just use Apex, but that defeats the benefit of the built in wired service.

How to specify different properties in a @wired method's object argument?

3
  • 1
    To clarify this, so you are looking to dynamically invoke the wired services based on the input, e.g., getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '$selectedRecordTypeId' } when you have selectedRecordTypeId and getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT} when you don't have one, is that correct?
    – Jayant Das
    Apr 25, 2019 at 21:25
  • @JayantDas - Yes Apr 26, 2019 at 14:06
  • Seems the platform cannot really handle a null value passed in that parameter. @tsalb's answer seems to be a good workaround for this limitation.
    – Jayant Das
    Apr 26, 2019 at 14:14

1 Answer 1

4

I first tried the following, but the linter complained: LWC1094: @wire expects a configuration object expression as second parameter.

_config = {}; // was trying to set this as the object

@wire(getRecordCreateDefaults, '$_config')
  accountCreateDefaults;

Which told me that it's not possible to bypass feeding in an object in its entirety. So then...it seems you might need two wires - one for each version (with and without recordTypeId) and defer runtime until you've calculated it like this:

@wire(getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT })
wiredAccountDefaults;

@wire(getRecordCreateDefaults, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '$selectedRecordTypeId' })
wiredAccountDefaultsWithRecordType;

connectedCallback() {
  const data = this.wiredAccountDefaultsWithRecordType // prefer recordTypeId version.
    ? this.wiredAccountDefaultsWithRecordType.data
    : this.wiredAccountDefaults.data;
  // process it
}

I too would like to know if there is a cleaner way, because this seems like an oversight to include optional parameters in @wire modules but not having a clean way to dynamically omit them.

2

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .