Thursday, June 9, 2016

How to use fieldsets in Apex class ?

Fieldset is a grouping of fields,  Fieldset is helpful to display group of fields in vf page. You can easily add, remove  fields from the Fieldsets. Suppose,you have requirement like : to show aFieldsets fields in your vf page .Then it will be used.  Fieldsets is very easy to use , you just drag and drop to arrange fields in fieldsets. Let's  Start with Simple Example :-

1- Create  a Class to get a fields based on the fieldsets.

public with sharing class fieldsetOfSobject {
     public static List<Schema.FieldSetMember> readFieldSet(String fieldSetName, String  ObjectName){
        Map<String, Schema.SObjectType> sobj= Schema.getGlobalDescribe(); 
        Schema.SObjectType sobjType= sobj.get(ObjectName);
        Schema.DescribeSObjectResult DescribeSObjectResultObj = sobjType.getDescribe();
        Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
        return fieldSetObj.getFields(); 
    }  
}

2-Now  use the above class method in your class.Suppose you have a object name 'TEST__c' and the fieldset  name   is 'CLASSIFICATION_ATTRIBUTES'.

List<String> Str =new List<String>();
List<Schema.FieldSetMember> fieldSetMemberList = fieldsetOfSobject.readFieldSet('CLASSIFICATION_ATTRIBUTES', 'TEST__c');
        
for(Schema.FieldSetMember fieldSetMemberObj : fieldSetMemberList){
            
          str.add(fieldSetMemberObj.getFieldPath());
 }

3- Now you get all the fields of your Fieldsets in str list.You have  to use it in your Vf  page according to Requirement.

No comments:

Post a Comment

Common interview questions for Salesforce developers

Name three Governor Limits .                                                                                    Description Synchronous ...