function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chuck wchuck w 

Dynamically requiring a field in a field set

I am employing field sets to display intormation on a VF page. A new requirement is to dynamically require a field within the field set, based on information from a record in an associated object.

For example, if Object1.checkbox1 = true, then the field Object2.DogName is required on the page. 

In the context of using a field set, is there a way to dynamically require Object2.DogName to be either required or not based on the value of Object1.checkbox? 

Any help is greatly appreciated! 
Ashish_SFDCAshish_SFDC
Hi Chuck, 


See the sample code in the blog below, 

http://foobarforce.com/2013/12/10/dynamic-field-sets/

Also See some sample code, 

http://salesforce.stackexchange.com/questions/1474/how-to-get-fieldset-fields-in-apex-dynamically-fieldset-name-is-not-static


Regards,
Ashish
Prabu MahalingamPrabu Mahalingam
Hi,
I would suggest you the following

Create 2 field sets one with that specific field as required and another field set as not required. Then render that part of the page using conditions

For Example:

   <apex:pageblocksection rendered="{!Object1.checkbox1 = true}">  
      <apex:repeat value="{!$ObjectType.Account.FieldSets[$Setup.AccountFieldSetSettings__c.FieldSet1]}" var="f">
           <apex:outputText value="{!Account[f]}" /><br/>
     </apex:repeat>

  <apex:pageblocksection rendered="{!Object1.checkbox1 = false}">
      <apex:repeat value="{!$ObjectType.Account.FieldSets[$Setup.AccountFieldSetSettings__c.FieldSet2]}" var="f">
           <apex:outputText value="{!Account[f]}" /><br/>
     </apex:repeat>


Cheers..!!!
nbknbk
Hello,

My approach is below.

class: 
global static boolean classvarflag {get;set;}
//make the classvarflag true/false based on obj1.checked field -- The assginment can be done in constructor or action function
based on the flagvalue the specific field is mandatory.
if(obj1.checked) classvarflag=true;

<apex:pageBlockSection id="obj2pgs" rendered="{!IF($ObjectType.Account.FieldSets.Obj2fsname}">0} >
            <apex:repeat value="{!$ObjectType.Account.FieldSets.Obj2fsname}" var="f" id="rep">
                 <apex:inputField required="{!classvarflag}"  value="{!supplier[f]}" rendered="{!IF(CONTAINS(Lower(f), 'obj2.fieldname') , true , false)}"/>
                  <apex:inputField   value="{!supplier[f]}" rendered="{!IF(CONTAINS(Lower(f), 'obj2.fieldname') , false, true)}"/>
            </apex:repeat>
</apex:pageBlocksection>


Thanks,
Krishna