Populate LWC from fieldset

lightning-recordeditformlightning-web-components

I am trying to populate an LWC form using field sets.
Currently I am populating a combo box with metadata records

Call Class to populate combo box and FieldSet

@AuraEnabled (cacheable=true)
public static List<objectFieldSet__mdt> getCmtFS(){
    return [SELECT Id, DeveloperName,fsName__c FROM objectFieldSet__mdt];
}

Combo box HTML

<lightning-card title="">
   <lightning-combobox
       name="Select:"
       label="Select:"
       placeholder="Choose an Object"
       value={value}
       onchange={handleCbupdate}
       options={object}>
   </lightning-combobox>
   <p></p>
</lightning-card>

Combo box JS

@wire(getCmtFsData)
wiredCmtObjectsData({ error, data }) {
   if (data) {
       for(i=0; i<data.length; i++)  {
           this.items = [...this.items ,{value: data[i].DeveloperName , label: data[i].DeveloperName} ];                                   
        }                
        this.error = undefined;
   } else if (error) {
       this.error = error;
   }
 }

  get object() {
      return this.items;
  }

Call Class to get fields for field set.

 gif( this.chosenValue == 'Account'){
    this.renderAccount = true;
    
    getAccountFieldSet()
    .then(result => {    
            this.accounts = JSON.parse(result);
            this.error = undefined;
        })
        .catch(error => {
            this.error = error;
            this.accounts = undefined;
      }

Field set Apex Class return fields in field set via a JSON string

public static String getAccountFieldList() {

    Map<String, String> returnFS = new Map<String,String>();

    List<Schema.FieldSetMember> listFieldSetFields = 
    Account.getSObjectType().getDescribe().fieldSets.getMap().get('RecordCreation').getFields();

    returnFS.put('FIELDSET_LIST', JSON.serialize(listFieldSetFields));
    return JSON.serialize(returnFS);

There is an Account, Contact, Invoice__c, Payment__c cmt records and each has a field set.
How can I used the fields returned from the apex class to populate a form for each object? These fields are all input fields

Best Answer

Solution to getting fields set to render

HTML

<template if:true={renderAccount}>
<lightning-layout multiple-rows="true">
    <lightning-layout-item padding="around-small" size="12">
        <lightning-record-edit-form object-api-name="Account" onsubmit={handleSubmit} onsuccess={handleSuccess}>
            <template for:each={accounts} for:item="acc"> 
                <lightning-input-field field-name={acc.fieldPath} key={acc.label}>
                </lightning-input-field>
            </template>
            <lightning-button
                class="slds-m-top_small"
                type="submit"
                label="Create Records">
            </lightning-button>
        </lightning-record-edit-form>
    </lightning-layout-item>
</lightning-layout>

JS

getAccountfieldSetFields(){
getAccountFieldSet()
    .then(result => {  
        let test = JSON.parse(result);
        this.accounts = JSON.parse(test.FIELDSET_LIST);

    })
    .catch(error => {
        this.error = error;
        this.accounts= undefined;

Apex Class

Map<String, String> returnFS = new Map<String,String>();

List<Schema.FieldSetMember> listFieldSetFields = 
Account.getSObjectType().getDescribe().fieldSets.getMap().get('RecordCreation').getFields();


returnFS.put('FIELDSET_LIST', JSON.serialize(listFieldSetFields));
return JSON.serialize(returnFS);
Related Topic