function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
edukondalu thodetiedukondalu thodeti 

how to get all the fields of an selected object(standard and custom) using lightning component

i had populate all the objects name in the org by using "iteration"    then my requirement is whenever i onclick any object  i need to display all fields related to that object  pls anyone help mei got the all the object names like these
AbhishekAbhishek (Salesforce Developers) 
You can make a Describe call to retrieve the metadata.
 
I suggest that you create a visualforce page with a dynamic picklist of sobjects, and a custom button with an apex method that returns all the available field names for that object.
 
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_describesobject.htm

For your reference, you can check the below blog,

https://developer.salesforce.com/forums/?id=9060G000000XbuqQAC

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Pankaj  Kumar MauryaPankaj Kumar Maurya
Lightning Web Component to fetch all objects and fields with the records.
<template>
  <lightning-combobox name="objName" options={objName} value={defaultVal} label="List of Salesforce Object Name" onchange={handleChange}></lightning-combobox>  
  <lightning-dual-listbox name="fieldName"
                            label="Select Fields"
                            source-label="Available"
                            selected-label="Selected"
                            field-level-help="Select Object Field Name"
                            options={fieldNames}
                            onchange={handleChange}
                            required-options={fieldNames}></lightning-dual-listbox>
    <br/>
    <template if:true={rowData}>
     <lightning-datatable data={rowData} key-field='id' columns={columns}></lightning-datatable>   
  </template>                       
</template>



//Js
import { LightningElement } from 'lwc';
import getObjectList from'@salesforce/apex/ObjectFieldRecordLst.getObjectList';
import getObjectFieldName from'@salesforce/apex/ObjectFieldRecordLst.getObjectFieldName';
import getRecordFromApex from'@salesforce/apex/ObjectFieldRecordLst.getRecordFromApex';
export default class ObjectFieldRecordLst extends LightningElement {
    objectName;
    defaultVal= 'User';
    objectFieldName;
    selectedFields = [];
    columns =[];
    rowData =[];
    //imperative call
    connectedCallback(){
        this.getObjectlst();
        this.getObjectFieldName();
    }

    getObjectlst(){
        getObjectList().then( (data) =>{
            console.log('data--',data);
            this.objectName = data;
            console.log('objName--',this.objectName);
        }).catch( (error) => {
            console.log('error',error);
        })
    }

    get objName(){
        let option= [];
        for(var key in this.objectName){
            option.push({label: this.objectName[key], value: this.objectName[key]});
        }
        return option;
    }

    handleChange(event){
        const field = event.target.name;
        if(field == 'objName'){
            this.defaultVal = event.detail.value;
            this.getObjectFieldName();
        } else if(field == 'fieldName'){
            let selectValue = event.detail.value;
            this.selectedFields = [];
            this.columns =[];
            for(var key in selectValue){
                this.selectedFields.push(selectValue[key]);
                this.columns.push({
                    label:selectValue[key],
                    fieldName:selectValue[key],
                    type: "text"
                });
            }
            this.getRecordFromApex();

        }
    }

    getObjectFieldName(){
        getObjectFieldName({objType : this.defaultVal}).then( (data) =>{
            this.objectFieldName = data;
        }).catch((error) => {
            console.log('error',error);
        });
    }

    get fieldNames(){
        let option = [];
        for(var key in this.objectFieldName){
            option.push({label: this.objectFieldName[key], value: this.objectFieldName[key]});
        }
        return option;
    }

    getRecordFromApex(){
        getRecordFromApex({lstFields : JSON.stringify(this.selectedFields), objectType : this.defaultVal}).then((data) =>{
            let tempList = []; 
            for (let row of data) {
                const flattenedRow = {}
                let rowKeys = Object.keys(row);
                rowKeys.forEach((rowKey) => {
                    const singleNodeValue = row[rowKey];
                    if (singleNodeValue.constructor === Object) {
                        this._flatten(singleNodeValue, flattenedRow, rowKey)
                    } else {
                        flattenedRow[rowKey] = singleNodeValue;
                    }
                });
                //push all the flattened rows to the final array 
                tempList.push(flattenedRow);
                this.rowData = this.handleLookupValues(tempList); 
            }
        }).catch( (error) => {
            console.log('error',error);
        });
    }

    _flatten = (nodeValue, flattenedRow, nodeName) => {
        let rowKeys = Object.keys(nodeValue);
        rowKeys.forEach((key) => {
            let finalKey = nodeName + '.' + key;
            flattenedRow[finalKey] = nodeValue[key];
        })
    }
    
    handleLookupValues(dataRecords) {
        dataRecords.forEach(row => {
            for (const col in row) {
                const curCol = row[col];
                if (typeof curCol === 'object') {
                    const newVal = curCol.Id ? ('/' + curCol.Id) : null;
                    this.flattenStructure(row, col + '.', curCol);
                    if (newVal === null) {
                        delete row[col];
                    } else {
                        row[col] = newVal;
                    }
                }
            }
        });

        return dataRecords;
    }

    flattenStructure(topObject, prefix, toBeFlattened) {
        for (const prop in toBeFlattened) {
            const curVal = toBeFlattened[prop];
            if (typeof curVal === 'object') {
                this.flattenStructure(topObject, prefix + prop + '.', curVal);
            } else {
                topObject[prefix + prop] = curVal;
            }
        }
    }
}

//Apex class
public class ObjectFieldRecordLst {
    @AuraEnabled
    public static list<String> getObjectList(){
        List<String> objName = new List<String>();
        for(Schema.SObjectType objType : Schema.getGlobalDescribe().Values()){
            objName.add(objType.getDescribe().getName());
        }
        return objName;
    }

    @AuraEnabled
    public static List<String> getObjectFieldName(String objType){
        List<String> objFieldName = new List<String>();
        Schema.SObjectType objectType = Schema.getGlobalDescribe().get(objType);
        Map<String, Schema.SObjectField> mapFieldList = objectType.getDescribe().fields.getMap(); 
        for(Schema.SObjectField fd : mapFieldList.values()) 
            objFieldName.add(fd.getDescribe().getName());
        return objFieldName;
    }

    @AuraEnabled
    public static List<sObject> getRecordFromApex(String lstFields,String objectType){
        List<sObject> lstOfRec = new List<sObject>(); 
        lstFields = lstFields.replace('[','').replace(']', '').replace('"','');
        String soqlQuery ='';
        String query = 'SELECT ';
        for(String str:lstFields.split(',')){
            system.debug('str--'+str);
            soqlQuery += ',' + str;
        }
        soqlQuery = soqlQuery.removeStart(',');
        query = query + soqlQuery + ' FROM '+ objectType;
        lstOfRec = Database.query(query);
        return lstOfRec;
    }
}

I hope you find this helpful. If it does, please mark as Best Answer to help others too.

Thanks.
Pankaj :-)