3

My requirement is to get picklist values from a record type if the object has more than one record type. Some examples are given with default record value passed. But in my requirement record type is not a default record type.

I tried below example but it didn't work. orderTypeOption is not populating with values. Use the recipe app from a sample gallery in my dev org.

wireGetPicklistValues.html

<template>
    <lightning-card title="WireGetPicklistValues" icon-name="custom:custom67">
        <div class="slds-m-around_medium" >
              Order type picklist
              <template if:true={orderTypeOption.data}>
                 <lightning-combobox
                    name="progress"
                    label="Order Type"
                    value={orderType}
                    options={orderTypeOption.data.values}
                    ></lightning-combobox>
              </template>
        </div>
        <template if:true={picklistValues.data}>
            <div class="slds-m-around_medium">
                <template for:each={picklistValues.data.values} for:item="item">
                    <lightning-input
                        key={item.value}
                        label={item.label}
                        data-value={item.value}
                        type="checkbox"
                    >
                    </lightning-input>
                </template>
            </div>
        </template>
        <template if:true={picklistValues.error}>
            <c-error-panel errors={picklistValues.error}></c-error-panel>
        </template>

        <c-view-source source="lwc/wireGetPicklistValues" slot="footer">
            Get the picklist values for a specified field. In this example,
            the&nbsp;
            <strong>Type</strong> field of the <strong>Account</strong> object.
        </c-view-source>
    </lightning-card>
</template>

wireGetPicklistValues.js

import { LightningElement, wire, track } from 'lwc';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Account.Type';
import ORDERTYPE_FIELD from '@salesforce/schema/Order.Type';

export default class WireGetPicklistValues extends LightningElement {
    @track orderType;

    @wire(getPicklistValues, {
        recordTypeId: '012000000000000AAA',
        fieldApiName: TYPE_FIELD
    })
    picklistValues;

     @wire(getPicklistValues, {
        recordTypeId: '$recordTypeId',
        fieldApiName: ORDERTYPE_FIELD
    })
    orderTypeOption;

    get recordTypeId() {
        // Returns a map of record type Ids 
        const rtis = this.objectInfo.data.recordTypeInfos;
        return Object.keys(rtis).find(rti => rtis[rti].name === 'Asia');
    }
}

2 Answers 2

8

Added based on comments

  1. The getter recordTypeId will not be invoked unless you use it in HTML like {recordTypeId} OR by invoking it in JS like this.recordTypeId().

  2. $recordTypeId is dynamic PROPERTY and not a method which will get value from method (getter).

  3. You are not setting objectInfo anywhere so recordTypeId() will never return anything even if used in HTML.

Try below JS:

import { LightningElement, wire, api, track } from 'lwc';
import ORDER_OBJECT from '@salesforce/schema/Order';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import { getPicklistValues } from 'lightning/uiObjectInfoApi';
import TYPE_FIELD from '@salesforce/schema/Order.Type';

export default class Poc extends LightningElement {
    @wire(getObjectInfo, { objectApiName: ORDER_OBJECT })
    getobjectInfo(result) {
        if (result.data) {
            const rtis = result.data.recordTypeInfos;
            this.recordTypeId = Object.keys(rtis).find((rti) => rtis[rti].name === 'Asia');
        }
    }

    @wire(getPicklistValues, { recordTypeId: '$recordTypeId', fieldApiName: TYPE_FIELD })
    orderTypeOption;
}

previous answer

In your example I see that you are using objectInfo but you are never fetching it.

Pls check below code sample:

import { LightningElement, wire, api } from 'lwc';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';

export default class Poc extends LightningElement {
    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    getobjectInfo(result) {
        console.log('result => ', JSON.stringify(result.data.recordTypeInfos));
    }
}

Here you will get the record types through result.data.recordTypeInfos. You will see below data in recordTypeInfos.

{
  "012000000000000AAA": {
    "available": true,
    "defaultRecordTypeMapping": false,
    "master": true,
    "name": "Master",
    "recordTypeId": "012000000000000AAA"
  },
  "0120K000000TO5FQAW": {
    "available": true,
    "defaultRecordTypeMapping": false,
    "master": false,
    "name": "P1r",
    "recordTypeId": "0120K000000TO5FQAW"
  },
  "0120K000000TO5KQAW": {
    "available": true,
    "defaultRecordTypeMapping": true,
    "master": false,
    "name": "P2r",
    "recordTypeId": "0120K000000TO5KQAW"
  }
}

You can find more info here


4
  • Thanks. Updated my question with the missing line and also mentioned where is the problem. But still, need to know how can I use recordTypeId in @wire
    – Rachit
    Aug 11, 2019 at 22:53
  • added in answer. Pls check Aug 12, 2019 at 4:14
  • Thanks @salesforce-sas for correcting me and it is working now. Just one confusion on this- JS function can execute in any order. Is 'wire' also following same as the function in terms of execution? if 'yes' then how can we make sure 'wire' getobjectInfo will execute always first?
    – Rachit
    Aug 12, 2019 at 12:49
  • In JS, code lines will execute in order. Its same case with wire also. However it will have data undefined 1st time but next time when dependent dynamic property changes, it will be invoked again. Refer to salesforce.stackexchange.com/questions/272681/… Aug 12, 2019 at 12:58
1

The above code worked for me after declaring recordTypeId

   import { LightningElement, wire, api, track } from 'lwc';
   import ORDER_OBJECT from '@salesforce/schema/Order';
   import { getObjectInfo } from 'lightning/uiObjectInfoApi';
   import { getPicklistValues } from 'lightning/uiObjectInfoApi';
   import TYPE_FIELD from '@salesforce/schema/Order.Type';

   export default class Poc extends LightningElement {
   recordTypeId=''
       @wire(getObjectInfo, { objectApiName: ORDER_OBJECT })
       getobjectInfo(result) {
          if (result.data) {
          const rtis = result.data.recordTypeInfos;
        this.recordTypeId = Object.keys(rtis).find((rti) => rtis[rti].name === 'Asia');
    }
}

@wire(getPicklistValues, { recordTypeId: '$recordTypeId', fieldApiName: TYPE_FIELD })
orderTypeOption;

}

You must log in to answer this question.

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