Hello friends, In this post you will see, how we can display salesforce sobject picklist and label dynamically without querying them. so let's get started,

Dynamically show labels and picklist fields in lightning component

In this code example, I am using single account object but you can you use multiple objects according to your requirement.

GetDynamicDescribedObjects.apxc

 public class GetDynamicDescribedObjects {

@AuraEnabled
public static String getObjectType() {
return getDescribedObjects(new List<String>{'Account'});
}

public static String getDescribedObjects( List<String> lstSObjectType ) {
Map<String, SObjectType> globalDescribe = Schema.getGlobalDescribe();
String allObjJSON = '{';
for( String sObjectType : lstSObjectType ) {
if( allObjJSON != '{' ) allObjJSON += ', ';
allObjJSON += '"' + sObjectType + '": ';
DescribeSObjectResult describeResult = globalDescribe.get(sObjectType).getDescribe();
Map<String, Schema.SObjectField> desribedFields = describeResult.fields.getMap();
String objJSON = '{';
for(String fieldName : desribedFields.keySet()){
Schema.SObjectField field = desribedFields.get(fieldName);
Schema.DescribeFieldResult f = field.getDescribe();
if( objJSON != '{' )
objJSON += ', ';
String helptext=f.getInlineHelpText()==null?'':f.getInlineHelpText();
System.debug('fieldName>>'+f.getName());
objJSON += '"' + f.getName() + '": ' + '{ "helpText" : "' + helptext + '", "label" : "' + f.getLabel() + '"';
if(field.getDescribe().getType() == Schema.DisplayType.PICKLIST || field.getDescribe().getType() == Schema.DisplayType.MultiPicklist){
List <Schema.PicklistEntry> picklistValues = field.getDescribe().getPickListValues();
List<String> pickListOptions = new List<String>();
pickListOptions.add('{ "label": "--None--", "value": null }');
for (Schema.PicklistEntry pe : picklistValues) {
pickListOptions.add('{ "label": "' + pe.getLabel() + '", "value": "' + pe.getValue() + '" }');
}
System.debug( '>>>> ' + fieldName + '>>>> ' + String.join(pickListOptions, ', ') );
objJSON += ', "picklistOptions": [' + String.join(pickListOptions, ', ') + ']';
}
objJSON += '}';
}
objJSON += '}';
allObjJSON += objJSON;
}
allObjJSON += '}';
System.debug( ' JSON STRING : ' + allObjJSON );
return allObjJSON;
}
}
DynamicDescribedObjects.cmp
 <aura:component controller="GetDynamicDescribedObjects" implements="flexipage:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="ObjectType" type="Object" />
<lightning:card title="Described Objects">
<label>
{!v.ObjectType.Account.Name.label}
</label>
<input id="txtAccountName" type="text" value="" class="slds-input"/>
<label>
{!v.ObjectType.Account.Rating.label}
</label>
<select id="ddlIncentive" class="slds-select">
<aura:iteration items="{!v.ObjectType.Account.Rating.picklistOptions}" var="option">
<option value="{!option.value}">{!option.label}</option>
</aura:iteration>
</select>
</lightning:card>
</aura:component>
DynamicDescribedObjectsController.js
 ({
doInit : function(component, event, helper) {
let action = component.get("c.getObjectType");
action.setCallback( helper, function( response ) {
if(component.isValid() && response.getState() === 'SUCCESS' ){
console.log(JSON.stringify(response.getReturnValue()));
component.set( 'v.ObjectType', JSON.parse( response.getReturnValue()));
}
else {
console.error( 'Error calling action with state: ' + response.getState());

}
});
$A.enqueueAction(action);
}
})

Output:

 Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.