Salesforce学习 Lwc(十四)【Get Record Type Id】

2021-01-12 10:19:31 浏览数 (1)

每个Object都有复数个Record Type,Lwc自定义开发过程中,项目上经常会遇到需要指定RecordType去登录数据,比如现在我们的Account表有【私企】和【国企】两个Object,我们开发的给私企用画面做成的数据的Record Type是私企的,给国企用画面做成的数据的Record Type是国企的,Record Type不需要显示在画面的情况下怎么实现呢。

【getObjectInfo】 →【recordTypeInfos】

实现方法:

代码语言:javascript复制
<template>
    <lightning-card title="Record Form with Record Type" icon-name="standard:account">
        <div if:true={objectInfo.data} class="slds-m-around_medium">
            <lightning-record-form object-api-name={objectApiName} 
                 record-type-id={recordTypeId}
                 fields={fields}>
            </lightning-record-form>
        </div>
    </lightning-card>
</template>
代码语言:javascript复制
import { LightningElement, api, wire, track } from 'lwc';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import PHONE_FIELD from '@salesforce/schema/Account.Phone';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';

export default class RecordFormWithRecordType extends LightningElement {
    // Flexipage provides recordId and objectApiName
    @api recordId;
    @api objectApiName;

    @track objectInfo;

    // Define fields to display in form
    // Industry field is a picklist
    fields = [NAME_FIELD, PHONE_FIELD, INDUSTRY_FIELD];

    @wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
    objectInfo;

    get recordTypeId() {
        const rtis = this.objectInfo.data.recordTypeInfos;
        return Object.keys(rtis).find(rti => rtis[rti].name === 'PrivateEnterprise');
    }
}

效果展示:

结果查询:

0 人点赞