Fieldset is a group of fields which can then be used in visualforce page or apex code. You have to only drag and drop the fields in a fieldset just like a page layout.
By accessing fields from a fieldset your code becomes dynamic as you can remove or add the fields by simply modifying the fieldset and there is no need to modify the code.
You can see how we can access fields directly in a visualforce page Here
Let us see how we can access the fields of a fieldset in a apex code.
First lets create a fieldset on contact and name it as "ContactFieldset".
Include fields by dragging and dropping in the area as shown in below screen shot. Have included three fields (Email, email optout and department)
Following piece of code forms query using filed api names fetched from a fieldset on contact.
Output of the debug statement is as shown below(it forms a proper query taking all the fields which were included in the fieldset)
By accessing fields from a fieldset your code becomes dynamic as you can remove or add the fields by simply modifying the fieldset and there is no need to modify the code.
You can see how we can access fields directly in a visualforce page Here
Let us see how we can access the fields of a fieldset in a apex code.
First lets create a fieldset on contact and name it as "ContactFieldset".
Include fields by dragging and dropping in the area as shown in below screen shot. Have included three fields (Email, email optout and department)
Following piece of code forms query using filed api names fetched from a fieldset on contact.
1
2
3
4
5
6
7
8
9
10
11
| Public void PlayWith_fieldset_Method(){ //form query from fields in fieldset String Querystring = 'select ' ; String fieldsApiNames; for (Schema.FieldSetMember fld :SObjectType.Contact.FieldSets.ContactFieldset.getFields()) { Querystring += fld.getFieldPath() + ', ' ; } Querystring += 'id from contact' ; System.debug( '**Query string**' +Querystring ); List<Contact> ConList = Database.query(Querystring); } |
Output of the debug statement is as shown below(it forms a proper query taking all the fields which were included in the fieldset)
No comments:
Post a Comment