We will be looking at how to get all the fields of an SObject dynamically in LWC.

Schema class contains lot of helpful methods for obtaining schema describe information.

Working with Schema class in Apex is pretty much straight forward but when it comes to LWC there are a few things that we need to handle in a different way.

Displaying fields in a picklist 

We will be looking at the bottle necks and all the work arounds that we will try to implement to get this working.

We will try to start off with the Apex Class and this is how it's gonna look.

@AuraEnabled(cacheable=true)
    public static List<String> getContactFields(){
        SObjectType contactType = Schema.getGlobalDescribe().get('Contact');
        Map<String,Schema.SObjectField> mfields = contactType.getDescribe().fields.getMap();

        List<String> sObjectDetailsList = new List<String>();
        for(String s : mfields.keySet()){
            sObjectDetailsList.add(String.valueOf(mfields.get(s)));
        }
        return sObjectDetailsList;
    }

If you notice it, the return type of the @AuraEnabled method is not Map<String, Schema.SObjectType> that's because AuraEnabled method cannot return data of type SObjectType.

So I thought of using List<String> as the return type of the method.

Then coming to the lightning web component, you will not find <lightning-select></lightning-select> there are a ton of reasons for that. One of the reason being LWC is built on Web Components standards, meaning, we can use all the entities that's used in Web Components in Lightning Web Components too.

So instead of <lightning-select></lightning-select> we can use native <select /> tags.

As an alternative we can use <lightning-combobox></lightning-combobox> but for some reason am inclined towards using the native tags, just because it's native and any changes done to these will automatically be available in LWC too.

Example, we can use Touch Events in LWC just because they can be used in Web Components.

Okay now, let us look at the complete web component.

This is the template file.

This is the JS file associated with the template file.

And this is the Apex class.

In case you have any questions, let me know in the comments below.

Interested in learning LWC Development with me? Subscribe to my course (use this if you are out of India).