1

In my Lightning Web Component I am passed the accountId and recordtypeId amongst a few other fields. I need to determine which recordtype the account is in order to display different parts in the component's html. This part gets me the recordtypeIDS if I pass it the Name, but I want it the other way around. I have the Id, but need the name.

@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
objectInfo;
get recordTypeId() {
// Returns a map of record type Ids
  const rtis = this.objectInfo.data.recordTypeInfos;
  return Object.keys(rtis).find(
    rti => rtis[rti].name === 'myAccountRecordType'
  );
}

I tried accessing the RecordTypeDeveloperName from the account passed in, but couldn't find a way to do that - if I could, that would save me having to look it up. Is that possible? It didn't like me adding the .RecordType.DeveloperName.value to the fields.

const FIELDS = [
"Account.Id",
"Account.FirstName",
"Account.LastName",
"Account.PersonEmail",
"Account.RecordType",
"Account.isPersonAccount"
];

//Account variables
@track fname;
@track lname;
@track aemail;
@track aid;
@track arectype;
@track arectypeid;
@track isPerson;

@wire(getRecord, { recordId: "$recordId", fields: FIELDS })
wiredAccount({ error, data }) {
if (data) {
  this.record = data;
  this.aid = this.record.fields.Id.value;
  this.fname = this.record.fields.FirstName.value;
  this.lname = this.record.fields.LastName.value;
  this.aemail = this.record.fields.PersonEmail.value;
  this.arectype = this.record.fields.RecordTypeId.value;
  this.isPerson = this.record.fields.isPersonAccount.value;
  this.error = undefined;
} else if (error) {
  this.error = error;
  this.record = undefined;
}
}

In my HTML I'm trying to determine whether the Account is a Person type or not. Using IsPersonAccount doesn't work, and I don't know how else to work out the type of account.

<div class="slds-form-element__control slds-m-around_medium">
  <lightning-record-view-form record-id={recordId} object-api-name="Account">
    <div class="slds-grid slds-box">
      <div class="slds-col slds-size_1-of-2">
        <lightning-output-field field-name="Name"></lightning-output-field>
        <template if:true={isPersonAccount}>
          <lightning-output-field field-name="PersonEmail"></lightning-output-field>
        </template>
      </div>
    </div>
  </lightning-record-view-form>
</div>

2 Answers 2

1
  1. The field for record type id is 'Account.RecordTypeId' and NOT "Account.RecordType" in FIELDS.
  2. In HTML you should be using class fields and not something else like isPersonAccount which is not even defined. Do not get confused with class field vs object field. You defined isPerson as class field and object field is isPersonAccount.

Here is the simplified version:

HTML:

    <div>recordType - {recordType}</div>
    <div>isPerson - {isPerson}</div>

JS:

const FIELDS = [ 'Account.Name', 'Account.RecordTypeId', 'Account.isPerson' ];

export default class Poc extends NavigationMixin(LightningElement) {
    recordId = '0010K00002IgBhRQAV';
    //Account variables
    @track name;
    @track recordType;
    @track isPerson;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    wiredRecord({ error, data }) {
        if (data) {
            this.record = data;
            this.name = this.record.fields.Name.value;
            this.recordType = this.record.fields.RecordTypeId.value;
            this.isPerson = this.record.fields.isPerson.value;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.record = undefined;
        }
    }
}
0

I also have this use case. I used to try getObjectInfo but failed.

In my workaround, I retrieve the Name, developerName, Id from RecordType sObject.

For example:

select Name, Id, DeveloperName from RecordType where Id = :recordTypeId

Or you could just soql the object's RecordTypeInfo by:

select Id, RecordTypeId, RecordType.Name, RecordType.DeveloperName from Custom_Object__c where RecordTypeID = :recorcTypeId

both cases need to write an Apex method and wire the method in your Js file.

You must log in to answer this question.

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