Hello Everyone, In this tutorial, I am going provide an example that explains, how to get field set using apex controller and expose them in lighting component. A field set is a grouping of fields that can be used in Visualforce pages and Lightning components. So let's get started,

Get field set dynamically in Lightning component


Step 1: Login to your Salesforce Org. and Navigate to Setup | Account | Field Sets. Click on New button.

Step 2: Login to your Salesforce Org. and Navigate to Setup | Account | Field Sets. Click on New button. Then input value in all required fields and hit the Save button.

Step 3: Drag fields in "In the Field Set" section that you want to show in Lighting component or Visualforce page. Save the layout.

Step 4: Open Developer Console and Navigate to File | New | Apex Class. Create a new apex class with following source code.

FieldSetHandler.apxc

 public class FieldSetHandler {

@AuraEnabled
public static String getJSONFieldSet(String sObjectName,String sFieldSetName){
String sFieldSet='';
sFieldSet=getFieldSet(sObjectName,sFieldSetName);
return sFieldSet;
}

public static String getFieldSet(String sObjectName,String fieldSetName) {
String result = '';
try{
SObjectType objToken = Schema.getGlobalDescribe().get(sObjectName);
Schema.DescribeSObjectResult d = objToken.getDescribe();
Map<String, Schema.FieldSet> FsMap = d.fieldSets.getMap();
system.debug('>>>>>>> FsMap >>> ' + FsMap);
if(FsMap.containsKey(fieldSetName))
for(Schema.FieldSetMember f : FsMap.get(fieldSetName).getFields()) {

if(result != ''){
result += ',';
}
String jsonPart = '{';
jsonPart += '"helpText":"' + gethelpText(sObjectName,f.getLabel()) + '",';
jsonPart += '"required":"' + (f.getDBRequired() || f.getRequired()) + '",';
jsonPart += '"type":"' + (f.getType()) + '",';
jsonPart += '"label":"' + (f.getLabel()) + '",';
jsonPart += '"name":"' + f.getFieldPath() + '"';
jsonPart += '}';
result += jsonPart;
}
}
catch(Exception e){
result += e.getLineNumber() + ' : ' + e.getMessage();
}
return '['+result+']';
}

public static string gethelpText(String sObjectName,String sField){
String helptext='';
String[] types = new String[]{sObjectName};
List<Schema.DescribeSobjectResult> results = Schema.describeSObjects(types);
for (Schema.DescribeSobjectResult result : results) {
Map<String, Schema.SObjectField> fieldMap = result.fields.getMap();
for(String fieldName : fieldMap.keySet()){
Schema.SObjectField field = fieldMap.get(fieldName);
Schema.DescribeFieldResult f = field.getDescribe();
if(f.getLabel()==sField){helptext=f.getInlineHelpText();
if(helptext==null)helptext=f.getLabel();
}
}
}
return helptext;
}
}

Step 5: Open Developer Console and Navigate to File | New | Lightning Component. Create a new lightning component called FieldSet. Replace the following source code in the component. 

 FieldSet.cmp

<aura:component controller="FieldSetHandler" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="fieldSetValues" type="List"/>
<br/><center><div style="font-weight:bold;font-size:15px;"><u>Field Set</u></div></center><br/>
<table class="slds-table slds-table_cell-buffer slds-cell-buffer_left slds-cell-buffer_right slds-table_bordered slds-table_striped">
<tr>
<aura:iteration items="{!v.fieldSetValues}" var="field">
<td>{!field.label}</td>
</aura:iteration>
</tr>
</table>
</aura:component>
FieldSetController.js
({
doInit: function(component, event, helper) {
helper.getFieldSet(component);
},
})
FieldSetHelper.js
({
getFieldSet :function(component){
var action = component.get("c.getJSONFieldSet");
action.setParams({
"sObjectName":"Account",
"sFieldSetName":"Demo"
})
action.setCallback(this, function(response) {
if(response.getState()=="SUCCESS"){
var fieldSetObj = JSON.parse(response.getReturnValue());
component.set("v.fieldSetValues", fieldSetObj);
console.log('get fieldset=>'+JSON.stringify(fieldSetObj));
}
else{
console.log(response.getError());
}
})
$A.enqueueAction(action);
}
})

Step 6: Preview your lightning component to see the output. 

Output:


Hope you like this post. For any query or suggestions, please feel free to comment.

Thank you.