+ Start a Discussion
Phuc 2Phuc 2 

Populate lightning-record-edit-form with field set

Hello All,
I have an LWC where I need to create a new record and I need to use a field set.

I have a combo box that when selected I want to query a CMT which has the field set. The combo box allows the user to select an object

How can I populate the html based on the combo box selection.  
Or can I just load them on load?

Just nee dto know how to show fields base don field set.

Thank you,
PriyaPriya (Salesforce Developers) 
Hi Phuc,

Do you want to display the list of fields based on the type of object selected from the drop down?

If yes then, try below:- 

VF page :- 
<apex:page controller="dynamicApex5">
 <apex:form id="theForm">
     <apex:pageBlock title="Select Object and Fields">
         <apex:pageBlockSection title="Select an Object" collapsible="false" columns="2">
             <apex:pageBlockSectionItem >
                 <apex:outputLabel value="Select an object:"></apex:outputLabel>
                 <apex:selectList value="{!objectName}" size="1" multiselect="false">
                     <apex:selectOptions value="{!selectedobject}">
                     <apex:actionSupport action="{!function}" event="onchange" reRender="fieldsPanel"/>  
             <apex:pageBlockSectionItem >
             <apex:outputPanel id="fieldsPanel">
                 <apex:outputLabel value="Select Fields:"> </apex:outputLabel>
                 <apex:selectList value="{!fields}" multiselect="true" size="3" >
                     <apex:selectOptions value="{!lsps}">

Controller :- 
public class dynamicApex5 {

   public list<SelectOption> selectedobject { get; set; }

    public String objectName { get; set; }    
    public set<string> flds{get;set;}
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public dynamicApex5(){
                selectedobject = new list<SelectOption>();  
                  selectedobject.add(new selectoption('','-None-'));
                  list<schema.SobjectType> lst=schemaMap.values();
                  for(schema.SobjectType ss:lst){
                      selectedobject.add(new selectoption(ss.getDescribe().getLocalName(),ss.getDescribe().getLabel()));
                 flds= new set<string>();
                  lsps=new list<SelectOption>();
      public String fields { get; set; }
       public list<SelectOption> lsps{get;set;}
       public void function() {
         if(objectName!=null || objectName!=''){
           Map<string,Schema.SObjectField> mpConField=schemaMap.get(objectName).getDescribe().fields.getMap();
                   for(string name:flds){
                   Schema.DescribeFieldResult fldResult=mpConField.get(name).getDescribe();
                    lsps.add(new SelectOption(fldResult.getName(),fldResult.getName()));


Please refer the below link for reference.

Hope it will be helpful.

Please mark it as best answer if it helps so that it can help other as well.

Thank you,
Priya Ranjan