Using FieldSet with Visualforce and Apex

One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its “Maintenance”, if New Filed is Added or needed to remove field we have to modify our code every time.

However, Thanks to Salesforce that we have “Field Set“.

With the Help of “Dynamic Visualforce Binding” and “Field Set” we can create effective Visualforce pages with ability to change Fields and its Sequence any time without modifying any code.

Let’s start with using Field set in Salesforce.

Step 1: Creating Field Set

First we need to create a field set. For this article, I am using custom object named “Question__c”.

We have to navigate to setting page of that object and then “Field Sets”. Create a new Field Set and add required field in Sequence as displayed in below image.

Create Field Set in Salesforce
Create Field Set in Salesforce

Assume that field set name is “Add_Question”.

Step 2: Use Field Set inside Visualforce using Dynamic Binding

Now, we are ready to use Field Set inside our Visualforce code. Following is code snippet of one of my Visual force page :

<apex:repeat value="{!$ObjectType.Question__c.FieldSets.Add_Question}"  var="f">
			<span  class="label" style="align:right"> {!f.label}  </span>
			<apex:inputField value="{!que[f.fieldPath]}" required="{!OR(f.required, f.dbrequired)}"/>
</apex:repeat>

As you can see in above code snippet, Field set can be accessed inside Visualforce like “{!$ObjectType.Question__c.FieldSets.Add_Question}”.

To get label of field, simply we can use “{!f.label}”.

To check, if field is required we can use “required” or “dbrequired” property.

We are getting fields dynamically by notation “{!que[f.fieldPath]}”. Where “que” is object defined inside Apex class.

Step 3 : Using Dynamic SOQL to get value – Optional

This step is Optional. If your VF page is working in “New or Insert” mode then there is no need to populate values inside variable “que”. However what if VF page works in Edit mode? How do we know, which fields are accessed? In this case we have to use Dynamic SOQL.

First we will get list of all fields used inside Field set using below method:

/**
*
* Get Fields from Field Set. It is not used on VF, It used to create Dynamic SOQL for Edit mode.
*/
public List getFields() {
	return SObjectType.Question__c.FieldSets.Add_Question.getFields();
}

Now, we will try to create Dynamic SOQL which will get record on basis of Id and will be assigned to object “que”.

/**
* On basis of field set, Create Dynamic SOQL
*/
private Question__c getQuestion_DynamiCSOQL(String quesId) {
	try
	{
		String query = 'SELECT ';
		for(Schema.FieldSetMember f : this.getFields()) {
			query += f.getFieldPath() + ', ';
		}
		query += 'Id, Name FROM Question__c WHERE Id = ''+quesId+''';
		return Database.query(query);
	}
	catch(Exception e)
	{
		Utility.addError('There is error while Fetching existing Question using Dynamic SOQL in Field Set. Error Detail - '+e.getMessage()); //This is Utility Method to display error message on VF Page
	}
	return null;
}

I hope, this article will help you. However, there are cases when you need to access Field Set even though Object name and Field Set name will be supplied dynamically.

Get Fieldset Fields in Apex dynamically when Object Name and FieldSet name is supplied at runtime

public static List readFieldSet(String fieldSetName, String ObjectName)
{
    Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe();
    Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName);
    Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();

    Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
    return fieldSetObj.getFields();
}

We can test above code in Developer Console as below:

 List fieldSetMemberList =  Util.readFieldSet('Account_FieldSet','Account');
for(Schema.FieldSetMember fieldSetMemberObj : fieldSetMemberList)
{
    system.debug('API Name ====>' + fieldSetMemberObj.getFieldPath()); //api name
    system.debug('Label ====>' + fieldSetMemberObj.getLabel());
    system.debug('Required ====>' + fieldSetMemberObj.getRequired());
    system.debug('DbRequired ====>' + fieldSetMemberObj.getDbRequired());
    system.debug('Type ====>' + fieldSetMemberObj.getType());
}

You can read Apex Documentation regarding FieldSet here

Posted

in

,

by


Related Posts

Comments

4 responses to “Using FieldSet with Visualforce and Apex”

  1. […] 145 : System admin has created a Visualforce for Account. In future he created few more new Fields. How could System admin can code Visualforce so that in future if any new field is added or existing field deleted. It should reflect in Visualforce without changing anycode? Ans : It can be done with help of Field Sets. Please read this article. […]

  2. Deep Avatar
    Deep

    Hi Shiva,

    I have to create a manage package contains one page which should be flexible so that I can add that it in any object(standard or Custom) page-layout as an Inline Page.
    As we know for use a page as Inline Page in the page-layout we should have to use standard controller of that object, so is there any way by which we can assign Standard controller attribute dynamically.

    Or is there any other way by which we can achieve this.

    Thanks in advance

    1. Jitendra Zaa Avatar

      Hi Deep,

      Unfortunately there is no way to have reusable inline visualforce page. Nearest solution I can think of – create a resusable Visualforce component with its own controller and reuse it by creating new Visualforce page each time inline. You can also try to use Tooling API to dynamically create Visualforce. There is idea pending and you can vote for it – https://success.salesforce.com/ideaView?id=08730000000BpCpAAK

  3. Shreya Bordia Avatar
    Shreya Bordia

    What if I delete a fieldset in managed org. I cannot recreate a fieldset with the same name. How to resolve this issue, because delete fieldsets are not available in the recycle bin.

Leave a Reply to Jitendra ZaaCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading