Exploring Dynamic Field Set usage
Field sets are really useful tool for assigning a group of selected fields from a given sObject to be used in code for any given use case.
Whether you wish to create a new record, update an existing record, show a small form, sort, filter or even map external service response Json nodes into sObject fields – Field Set will be a handy tool.
Creating a Field Set
Find Field Set option on on all standard objects that support custom fields or any Custom Object.
- Account sObject Create Field Set Example :
- Lightning > Setup > Object Manager > Account > Field Sets
- Classic > Setup > Customize > Accounts > Field Set.
- You can now drag and drop the fields you want to include.
Like you would do for page layout – Fieldset allowing you to set your group of fieldset members in same look and feel editor.
Useful Apex Methods
Once your field set is defined you can easily get the definition and content from Apex:
Schema.FieldSet myFieldset = Schema.SObjectType.Account.fieldSets.getMap().get('field_set_name');
This will get the FieldSet from a defined sObject eg. “Account”.
Retrieve Fieldset Members dynamically from ObjectName and Fieldset Name:
public static List<Schema.FieldSetMember> getFieldSetMembers (String sObjectName,String fieldSetName) { /* SObject Describe */ Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get(sObjectName); Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe(); /* Field Set */ List<Schema.FieldSetMember> fieldSetMemberList = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName).getFields(); return fieldSetMemberList; }
Describe Field Set Members
To get useful information like the ApiName, Label and Type from all field member just iterate over the field members list and use the following built in method:
public static List<Map<String,Object>> getFieldsMaps (String objectName, String fieldSetName) { List<Map<String,Object>> fieldsOutput = new List<Map<String,Object>>(); List<Schema.FieldSetMember> fieldSetMemberList = getFieldSetMembers(objectName,fieldSetName); for(Schema.FieldSetMember field:fieldSetMemberList) { Map<String,Object> fieldMap = new Map<String,Object> { 'fieldName' => field.getFieldPath(), 'fieldLabel' => field.getLabel(), 'fieldType' => field.getType() }; fieldsOutput.add(fieldMap); } return fieldsOutput; }
Building Dynamic Queries with Field Set
A common use case will be to add the Fieldset fields dynamically into a SOQL query:
Set<String> fieldApiNames = new Set<String> (); for(Schema.FieldSetMember field:fieldSetMemberList) { // collect unique api field names fieldApiNames.add(field.getFieldPath()); } //adding fields api names into the query String queryString = 'SELECT ' + String.join(new List<String>(fieldApiNames), ', '); queryString += ' FROM ' + sobjectName; List<SObject> records = Database.query(queryString);
Describe Field Set Fields with key, value pairs
To complete the process and get the values of the fields from the record itself – we can iterate over the records returned do something like the following:
// getting value per field in fieldset for(Schema.FieldSetMember field:fieldSetMemberList) { String fieldApiName = field.getFieldPath(); Map<String,Object> fieldMap = new Map<String,Object> { fieldApiName => records[0].get(fieldApiName) }; // Alternative to get sObjectField from Field Api Name // Schema.SObjectField field = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().get(fsm.getFieldPath()); }
Just always make sure you keep a good naming conventions as when you start having many Fieldsets on every sObject it can get daunting in times.