Last active
May 27, 2022 08:47
-
-
Save sfcure/4a3aba223989c5cd842f969cf81267a8 to your computer and use it in GitHub Desktop.
Get field labels dynamically in the lightning component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AccountEditController { | |
@AuraEnabled | |
public static String getObjectType() { | |
// Passing the sObject name in the method, it could be multiple objects too | |
return Utility.getDescribedObjects( new List<String>{'Account'} ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="AccountEditController"> | |
<aura:attribute name="ObjectType" type="Object"/> | |
<aura:attribute name="account" type="Account" | |
default="{ Name: 'Test Account', Type: 'Business', Phone: '+44 34522346', Client_Status__c: 'Not Active' }"/> | |
<aura:handler name="init" value="{!this}" | |
action="{!c.doInit}"/> | |
<div class="c-container"> | |
<lightning:layout> | |
<lightning:layoutItem padding="around-small"> | |
<lightning:input aura:id="name" label="{!v.ObjectType.Account.Name.label}" type="text" value="{!v.account.Name}"/> | |
</lightning:layoutItem> | |
<lightning:layoutItem padding="around-small"> | |
<lightning:select aura:id="type" label="{!v.ObjectType.Account.Type.label}" value="{!v.account.Type}"> | |
<aura:iteration items="{!v.ObjectType.Account.Type.picklistOptions}" var="option"> | |
<option value="{!option.value}" text="{!option.label}"/> | |
</aura:iteration> | |
</lightning:select> | |
</lightning:layoutItem> | |
</lightning:layout> | |
<lightning:layout> | |
<lightning:layoutItem padding="around-small"> | |
<lightning:input aura:id="phone" type="phone" label="{!v.ObjectType.Account.Phone.label}" value="{!v.account.Phone}"/> | |
</lightning:layoutItem> | |
<lightning:layoutItem padding="around-small"> | |
<lightning:input aura:id="text" type="clientStatus" label="{!v.ObjectType.Account.Client_Status__c.label}" value="{!v.account.Client_Status__c}"/> | |
</lightning:layoutItem> | |
</lightning:layout> | |
</div> | |
</aura:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit : function(component, event, helper) { | |
let action = component.get( "c.getObjectType" ); | |
action.setCallback( helper, function( response ) { | |
if ( component.isValid() && response.getState() === 'SUCCESS' ) { | |
// Parse the JSON string into an object | |
component.set( 'v.ObjectType', JSON.parse( response.getReturnValue() ) ); | |
console.table( JSON.parse( response.getReturnValue() ) ); | |
} else { | |
console.error( 'Error calling action "' + actionName + '" with state: ' + response.getState() ); | |
} | |
}); | |
$A.enqueueAction( action ); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utility { | |
public static String getDescribedObjects( List<String> lstSObjectType ) { | |
// Globally desribe all the objects | |
Map<String, SObjectType> globalDescribe = Schema.getGlobalDescribe(); | |
// Create a JSON string with object field labels and picklist values | |
String allObjJSON = '{'; | |
// Iterate over the list of objects and describe each object | |
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() ) { | |
// Descirbe the field | |
Schema.SObjectField field = desribedFields.get( fieldName ); | |
Schema.DescribeFieldResult f = field.getDescribe(); | |
if( objJSON != '{' ) | |
objJSON += ', '; | |
// Get the field label and append in the JSON string | |
objJSON += '"' + f.getName() + '": ' + '{ "label" : "' + f.getLabel() + '"'; | |
// if it's a picklist field then also add the picklist options | |
if( field.getDescribe().getType() == Schema.DisplayType.PICKLIST ){ | |
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; | |
} | |
// Close the object in the JSON String | |
allObjJSON += '}'; | |
System.debug( ' JSON STRING : ' + allObjJSON ); | |
return allObjJSON; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment