0

I'm new to salesforce development and I'm trying to dynamically create fields based on the field name selected. I'm not sure how to achieve this.

I have a picklist of fieldnames for a Lead object in a visualforce page. When a fieldname is selected, I want to re-render the fieldvalue type based on the fieldname selected. For eg.

If selected fieldName is 'Lead.Rating', I want to display a picklist whereas if the selected fieldname is 'Lead.FirstName' I want to display a textbox.

I have read about dynamic component and dynamic bindings but I'm not sure which to use in my context. Any help would be appreciated

Edit:

VF code snippet:

                        <apex:column headerValue="Field" style="width:50px">
                            <apex:actionRegion >
                                <apex:selectList style="width:160px" value="{!filter.Record_Field__c}" size="1">
                                    <apex:selectOptions value="{!LeadFields}"/>
                                    <apex:actionSupport event="onchange" reRender="fieldpanel"/>
                                </apex:selectList>
                            </apex:actionRegion>
                        </apex:column>

                        <apex:column headerValue="Value">
                            <apex:outputPanel id="fieldpanel">
                                <apex:inputField style="width:150px" value="{!filter.Record_Value__c}" id="ffield" rendered="true" />

                               <apex:actionRegion>
                                    <apex:inputField style="width:150px" id="value1" value="{!selectedObj[filter.Record_Field__c]}"
                                    rendered="{!IF(filter.Record_Field__c != null , true , false)}">
                                    </apex:inputField>
                                </apex:actionRegion>


                            </apex:outputPanel>

Here selectedObj is a 'Lead' sObject.

This works well for rendering the correct input type. however I want to assign the value obtained from {!selectedObj[filter.Record_Field__c]} to filter.Record_Value__c. How can I do that?

  • 1
    If you have a wish to get a good ans then share your code – Santanu Boral Jan 6 '17 at 5:32
  • Are you trying to just display one input that you dynamically select? One output? Please edit your post to be more specific and detailed about what you are trying to achieve and what you have tried already. – Adrian Larson Jan 6 '17 at 5:40
2

You can achieve that with <apex:inputField>, pass the FieldName dynamically in the value attribute of <apex:inputField> and it will render your required field on VF.

for example

<apex:inputField value="{!yoursobject[fieldName]}" />

like

<apex:inputField value="{!leadRec['Rating']}" />

let me know if you need more detail

  • Thanks that helps. Is there a way to assign the value from this inputfield to another custom object. Basically, I want to assign the value from {!leadRec['Rating']} to my custom object. Eg. If lead.rating='Hot' , I want to assign it to a custom object filter__c.value. – gerad26 Jan 6 '17 at 7:40
  • You can do that easily, the value you choosed from this dynamic field, can be referenced in controller side by leadRec.rating so just pass that value to your desired object. like Filter__c.value = leadRec.rating – user2809299 Jan 6 '17 at 7:48
  • I see. The problem I have is that I can have multiple filter rows. And the 'selectedObj' is updated whenever I move to other rows. The selectedObj is a placeholder for getting the input type. Can I assign the value to filter__c on an onchange event to capture the current value for selectedObj? Does that make sense? – gerad26 Jan 6 '17 at 7:55
  • I got your requirement, so you are building a custom filtration where user apply the filter and it save the applied filtration to a separate object. There are many ways to achieve that. one thing is whenever user provide the value capture that value on JS end and then when user finish with the filtration you can send that combined filter condition to controller and save that. second thing which you suggest on change event of the input field send the value into controller and perform further action, now if you want to perform certain controller action on change event then second way is good. – user2809299 Jan 6 '17 at 9:18
  • Recently I built a custom Report builder where user can create Report on any object with any filter condition and I build the entire logic on client side with action functions, once user finish with that I saved the user applied criteria into a separate object which can be retried by user next time like saved Reports. – user2809299 Jan 6 '17 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.