3

I am running into an issue getting the recordType Name to display on my LWC. Any advice would be greatly appreciated.

clientside js:

import { LightningElement } from "lwc";
import getTrackingData from "@salesforce/apex/OneHourTranslationClientApex.getTrackingTranslationObject";

const columns = [
  { label: "Name", fieldName: "Name", Type: "text" },
  { label: "Type", fieldName: "recordType.Name", type: "text" },
  { label: "language", fieldName: "Language_Options__c", type: "text" },
  { label: "OHT Project Id", fieldName: "Project_Id__c", type: "text" },
  { label: "Translation Complete", fieldName: "Project_Completed__c", type: "text" }
];

export default class TranslationTrackingDataTable extends LightningElement {
  data = [];
  columns = columns;

  async connectedCallback() {
    const data = await getTrackingData();
    this.data = data;
    console.log(`this is the data ${JSON.stringify(this.data)}`);
  }
}

APEX Serverside Code:

  @AuraEnabled(cacheable=true)
  public static List<TranslationTracking__c> getTrackingTranslationObject() {
    List<TranslationTracking__c> TranslationObjects = [SELECT Name, RecordType.Name , Expertise_Options__c, Language_Options__c, Project_Id__c, Project_Completed__c FROM TranslationTracking__c];
    for (TranslationTracking__c translation : TranslationObjects) {
      translation.Language_Options__c = languageTranslationStore.getLanguageName(translation.Language_Options__c);
    }
    return TranslationObjects;
  }

enter image description here

this should display the recordType.Name for each record.

3
  • 1
    Try fieldName: "RecordType.Name" (as this is case sensitive) but if that doesn't work you will have to flatten the data in JS. AFAIC data table OOBE doesn't support unpacking dot notation like this
    – zaitsman
    Aug 10, 2020 at 1:09
  • Thanks! Did not work. I will manipulate in clientside js Aug 10, 2020 at 2:14
  • I have tried to modify the on the js side by adding a .foreach loop however I am getting the following issue: [Cannot add property recordType, object is not extensible] the value I am assigning to recordType is the string value of what I want to output in the datatable. Aug 10, 2020 at 3:08

1 Answer 1

5

Flatten your data in the front end as shown below

const columns = [
  { label: "Name", fieldName: "Name", Type: "text" },
  { label: "Type", fieldName: "recordTypeName", type: "text" },
  { label: "language", fieldName: "Language_Options__c", type: "text" },
  { label: "OHT Project Id", fieldName: "Project_Id__c", type: "text" },
  { label: "Translation Complete", fieldName: "Project_Completed__c", type: "text" }
];

if (this.data) {
    let newArray = [];
    this.data.forEach(tracking => {
        let newTracking = {};
        newTracking.Name = tracking.Name;
        newTracking.recordTypeName = recordTypeName.RecordType.Name;
        newTracking.Expertise_Options__c = newTrackingExpertise_Options__c;
        ..
        ..
        // and so on for other fields
        newArray.push(newTracking);
    });
    this.tableData = newArray;
}

Now tie back the tableData to the lightning:datatable

You can also use Array Destructuring Assignment and map function to simplify. Credit to sfdcfox pointing me towards this,

this.tableArray = this.data.map(row => ({ ...row,recordTypeName:row.RecordType.Name}))
3
  • 3
    this.tableArray = this.data.map(row => ({ ...row,recordTypeName:row.RecordType.Name})) has the same effect.
    – sfdcfox
    Aug 10, 2020 at 4:46
  • Ah that’s neat! Forgot about this array array destructuring Aug 10, 2020 at 4:51
  • Thank you all my issue has been solved using your suggestions. Aug 10, 2020 at 13:51

You must log in to answer this question.

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