Skip to main content
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,

Michael
1 respuesta
  1. 28 may 2022, 9:07
    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:selectOptions>

    <apex:actionSupport action="{!function}" event="onchange" reRender="fieldsPanel"/>

    </apex:selectList>

    </apex:pageBlockSectionItem>

    <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}">

    </apex:selectOptions>

    </apex:selectList>

    </apex:outputPanel>

    </apex:pageBlockSectionItem>

    </apex:pageBlockSection>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

    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()));

    selectedobject.sort();

    }

    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();

    flds=mpConField.keySet();

    for(string name:flds){

    Schema.DescribeFieldResult fldResult=mpConField.get(name).getDescribe();

    lsps.add(new SelectOption(fldResult.getName(),fldResult.getName()));

    lsps.sort();

    }

    }

    }

    }

    Please refer the below link for reference.

    https://success.salesforce.com/answers?id=90630000000h1YfAAI

    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
0/9000