{"id":3278,"date":"2013-05-03T19:14:58","date_gmt":"2013-05-03T13:44:58","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=3278"},"modified":"2015-07-19T22:08:14","modified_gmt":"2015-07-19T22:08:14","slug":"using-fieldset-with-visualforce-and-apex","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/visualforce\/using-fieldset-with-visualforce-and-apex\/","title":{"rendered":"Using FieldSet with Visualforce and Apex"},"content":{"rendered":"<p style=\"text-align: justify;\">One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its &#8220;Maintenance&#8221;\u009d, if New Filed is Added or needed to remove field we have to modify our code every time.<\/p>\n<p>However, Thanks to Salesforce that we have &#8220;<a title=\"Salesforce Field Set Documentation\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/pages\/index_Left.htm#CSHID=pages_dynamic_vf_field_sets.htm|StartTopic=Content%2Fpages_dynamic_vf_field_sets.htm|SkinName=webhelp\" rel=\"nofollow\">Field Set<\/a>&#8220;\u009d.<\/p>\n<p style=\"text-align: justify;\">With the Help of &#8220;Dynamic Visualforce Binding&#8221;\u009d and &#8220;Field Set&#8221;\u009d we can create effective Visualforce pages with ability to change Fields and its Sequence any time without modifying any code.<\/p>\n<p>Let&#8217;s start with using Field set in Salesforce.<\/p>\n<p><strong>Step 1: Creating Field Set<\/strong><\/p>\n<p style=\"text-align: justify;\">First we need to create a field set. For this article, I am using custom object named &#8220;Question__c&#8221;\u009d.<\/p>\n<p style=\"text-align: justify;\">We have to navigate to setting page of that object and then &#8220;Field Sets&#8221;\u009d. Create a new Field Set and add required field in Sequence as displayed in below image.<\/p>\n<figure id=\"attachment_3281\" aria-describedby=\"caption-attachment-3281\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\" wp-image-3281 \" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2013\/05\/Create-Field-Set-in-Salesforce.png?resize=635%2C263&#038;ssl=1\" alt=\"Create Field Set in Salesforce\" width=\"635\" height=\"263\" \/><\/a><figcaption id=\"caption-attachment-3281\" class=\"wp-caption-text\">Create Field Set in Salesforce<\/figcaption><\/figure>\n<p>Assume that field set name is &#8220;Add_Question&#8221;\u009d.<!--more--><\/p>\n<p><strong>Step 2: Use Field Set inside Visualforce using Dynamic Binding<\/strong><\/p>\n<p style=\"text-align: justify;\">Now, we are ready to use Field Set inside our Visualforce code. Following is code snippet of one of my Visual force page :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;apex:repeat value=&quot;{!$ObjectType.Question__c.FieldSets.Add_Question}&quot;  var=&quot;f&quot;&gt;\r\n\t\t\t&lt;span  class=&quot;label&quot; style=&quot;align:right&quot;&gt; {!f.label}  &lt;\/span&gt;\r\n\t\t\t&lt;apex:inputField value=&quot;{!que&#x5B;f.fieldPath]}&quot; required=&quot;{!OR(f.required, f.dbrequired)}&quot;\/&gt;\r\n&lt;\/apex:repeat&gt;\r\n<\/pre>\n<p style=\"text-align: justify;\">As you can see in above code snippet, Field set can be accessed inside Visualforce like &#8220;{!$ObjectType.Question__c.FieldSets.Add_Question}&#8221;\u009d.<\/p>\n<p style=\"text-align: justify;\">To get label of field, simply we can use &#8220;{!f.label}&#8221;\u009d.<\/p>\n<p style=\"text-align: justify;\">To check, if field is required we can use &#8220;required&#8221;\u009d or &#8220;dbrequired&#8221;\u009d property.<\/p>\n<p style=\"text-align: justify;\">We are getting fields dynamically by notation &#8220;{!que[f.fieldPath]}&#8221;\u009d. Where &#8220;que&#8221;\u009d is object defined inside Apex class.<\/p>\n<p><strong>Step 3 : Using Dynamic SOQL to get value \u2013 Optional<\/strong><\/p>\n<p style=\"text-align: justify;\">This step is Optional. If your VF page is working in &#8220;New or Insert&#8221;\u009d mode then there is no need to populate values inside variable &#8220;que&#8221;\u009d. 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.<\/p>\n<p>First we will get list of all fields used inside Field set using below method:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/**\r\n*\r\n* Get Fields from Field Set. It is not used on VF, It used to create Dynamic SOQL for Edit mode.\r\n*\/\r\npublic List getFields() {\r\n\treturn SObjectType.Question__c.FieldSets.Add_Question.getFields();\r\n}\r\n<\/pre>\n<p style=\"text-align: justify;\">Now, we will try to create Dynamic SOQL which will get record on basis of Id and will be assigned to object &#8220;que&#8221;.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/**\r\n* On basis of field set, Create Dynamic SOQL\r\n*\/\r\nprivate Question__c getQuestion_DynamiCSOQL(String quesId) {\r\n\ttry\r\n\t{\r\n\t\tString query = 'SELECT ';\r\n\t\tfor(Schema.FieldSetMember f : this.getFields()) {\r\n\t\t\tquery += f.getFieldPath() + ', ';\r\n\t\t}\r\n\t\tquery += 'Id, Name FROM Question__c WHERE Id = ''+quesId+''';\r\n\t\treturn Database.query(query);\r\n\t}\r\n\tcatch(Exception e)\r\n\t{\r\n\t\tUtility.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\r\n\t}\r\n\treturn null;\r\n}\r\n<\/pre>\n<p style=\"text-align: justify;\">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.<\/p>\n<p><strong>Get Fieldset Fields in Apex dynamically when Object Name and FieldSet name is supplied at runtime<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static List readFieldSet(String fieldSetName, String ObjectName)\r\n{\r\n    Map&lt;String, Schema.SObjectType&gt; GlobalDescribeMap = Schema.getGlobalDescribe();\r\n    Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName);\r\n    Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();\r\n\r\n    Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);\r\n    return fieldSetObj.getFields();\r\n}\r\n<\/pre>\n<p>We can test above code in Developer Console as below:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n List fieldSetMemberList =  Util.readFieldSet('Account_FieldSet','Account');\r\nfor(Schema.FieldSetMember fieldSetMemberObj : fieldSetMemberList)\r\n{\r\n    system.debug('API Name ====&gt;' + fieldSetMemberObj.getFieldPath()); \/\/api name\r\n    system.debug('Label ====&gt;' + fieldSetMemberObj.getLabel());\r\n    system.debug('Required ====&gt;' + fieldSetMemberObj.getRequired());\r\n    system.debug('DbRequired ====&gt;' + fieldSetMemberObj.getDbRequired());\r\n    system.debug('Type ====&gt;' + fieldSetMemberObj.getType());\r\n}\r\n<\/pre>\n<p><a title=\"Fieldset Documentation\" href=\"http:\/\/www.salesforce.com\/us\/developer\/docs\/pages\/index_Left.htm#CSHID=pages_dynamic_vf_field_sets.htm|StartTopic=Content%2Fpages_dynamic_vf_field_sets.htm|SkinName=webhelp\" rel=\"nofollow\">You can read Apex Documentation regarding FieldSet here<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the disadvantages comes up with Custom Page or Overriding New or Edit button with Visualforce page is its &#8220;Maintenance&#8221;\u009d, 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 &#8220;Field Set&#8220;\u009d. With the Help of &#8220;Dynamic Visualforce Binding&#8221;\u009d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,18],"tags":[337,331,336],"class_list":["post-3278","post","type-post","status-publish","format-standard","hentry","category-apex","category-visualforce","tag-apex","tag-salesforce","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3773,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-faq-part-19\/","url_meta":{"origin":3278,"position":0},"title":"Salesforce interview questions &#8211; Part 19","author":"Jitendra","date":"December 16, 2014","format":false,"excerpt":"Salesforce interview questions for developers and admins around Apex, Visualforce, getting Salesforce object name on basis of Id, Apex API limits","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2501,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/very-useful-tips-and-tricks-of-the-apex-salesforce-interview-questions-part-4\/","url_meta":{"origin":3278,"position":1},"title":"Latest Salesforce Interview Questions &#8211; Part 4 &#8211; Related to Dynamic Apex","author":"Jitendra","date":"November 27, 2011","format":false,"excerpt":"Most Frequently Asked interview questions of Apex, Dynamic Apex, SOSL, Visualforce, SOQL in Salesforce.com SFDC","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions\/","url_meta":{"origin":3278,"position":2},"title":"Salesforce Interview Questions &#8211; Part 1","author":"Jitendra","date":"May 6, 2010","format":false,"excerpt":"Set of most often asked questions on the salesforce.com developement","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4870,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/resolve-24-hour-apex-email-limit-error-in-salesforce\/","url_meta":{"origin":3278,"position":3},"title":"Resolve 24 hour Apex email limit error in Salesforce","author":"Jitendra","date":"October 13, 2015","format":false,"excerpt":"How to design and architect Salesforce application so that 24 hour Apex email limit error could be resolved and have reporting capabilities on emails sent from Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Resolve 24 hour Apex email limit error in Salesforce","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/10\/Resolve-24-hour-Apex-email-limit-error-in-Salesforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2470,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/salesforce-tutorial-create-simple-ajax-based-visualforce-page\/","url_meta":{"origin":3278,"position":4},"title":"Salesforce Tutorial &#8211; Create Simple Ajax based Visualforce page","author":"Jitendra","date":"October 17, 2011","format":false,"excerpt":"Salesforce Tutorial - Step by step tutorial to create AJAX based application in visualforce page with Apex class","rel":"","context":"In &quot;Web Technology&quot;","block_context":{"text":"Web Technology","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/"},"img":{"alt_text":"Simple AJAX demo in salesforce using visualforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/10\/Simple-AJAX-demo-in-salesforce-using-visualforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2987,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-10\/","url_meta":{"origin":3278,"position":5},"title":"Salesforce Interview Questions \u2013 Part 10","author":"Jitendra","date":"August 2, 2012","format":false,"excerpt":"This Part of Salesforce interview question series depict on browser compatibility issue (Internet Explorer 9) and Visualforce normally for AJAX, Group By and Having Clause. 91. How to add the Document Header in Visualforce page? Ans : Directly there is no way to add the document type in visualforce. However\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=3278"}],"version-history":[{"count":1,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3278\/revisions"}],"predecessor-version":[{"id":4739,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3278\/revisions\/4739"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=3278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=3278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=3278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}