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
ScriptMonkeyScriptMonkey 

Dynamic Form Creation Problem.

I'm looking to have an object store a list of fields to be made into a form, with "type" being text, textarea, file, hidden, checkbox, etc.  I'm not sure how to best accomplish this.  I can think of two ways, but both have notable problems.

 

Option one:

define a set of boolean variables and then

<apex:inputFile rendered="{!isFile}" value="{!blahblahblah}" />
<apex:inputText rendered="{!isText}" value="{!blahblahblah}" />
<apex:inputHidden rendered="{!isHidden}" value="{!blahblahblah}" />

 

etc. (there are seven options, I believe, so you're looking at 7 boolean tests PER field, so when making a 50 field form, that's 350 boolean tests.. page loads too slow)

 

Option two:

Where the booleans above are defined, just have it actually make a string with the HTML input command, and the VF page just have something like:

<apex:repeat value="{!fieldOutputs}" var="output" id="theRepeat">
<apex:outputText escape="false" value="{!output}" /><br />
</apex:repeat>

 


That worked fine for making the page, and ran fast (because you can "else If" in apex) but now I don't have a way to get the data back to the controller when I click the save button.

 

I would greatly appreciate any help that can be offered, thanks!

 

ScriptMonkey

Best Answer chosen by Admin (Salesforce Developers) 
stephanmstephanm

I assume that you've called support to enable the dynamic visualforce components pilot? Also that you're on the latest API version?

All Answers

stephanmstephanm

You may want to try an approach using dynamic visualforce components. See the dev guide for more info.

ScriptMonkeyScriptMonkey

I looked into that and while I was ecstatic, thinking it was the right answer, sadly it's not.

 

Dynamic components don't give you any functionality that we didn't already have, it just moves them to Apex.

 

To use the Dynamic Visualforce Component, I need to do something like this:

<apex:dynamicComponent componentValue="{!componentObj}" />

 The problem is that componentObj needs to either be defined with get/set functions, or just be the function getComponentObj().

 

this is the problem, because the definition of the variable, or function, must declare the type of value, IE:

public Component.Apex.inputText componentObj{ get; set; }

or

public Component.Apex.inputText getComponentObj()
{
//return something
}

 

 The problem is, I don't know if I'm going to use an inputText or inputTextarea or inputHidden, etc..

I need to be able to change the TYPE of component, not just modify the attributes in apex, but the type has to be set in stone to use dynamic components, it's just set in stone in the variable type, rather than the component type.

 

If I'm missing a use case then please, let me know.  Also, if you can think of another way, if I'm not, that I can do this, I'm open to ideas.

ScriptMonkeyScriptMonkey

Ok, I did more research and found out that I can make a dynamic "outputPanel" and give it "child objects" which can be totally dynamic.  sounds great, concept should work, even based on the examples 

 

Here's the problem:

This is the sample exactly from the docs:

public Component.Apex.PageMessage getCheckDueDate() 
    { 
        Component.Apex.SectionHeader sectionHeader= new Component.Apex.SectionHeader(); 
        sectionHeader.title='This form was due on 03/06/2011!'; 
        date myDate = date.today(); 
        date dueDate = date.newInstance(2011, 3, 6); 

        boolean dueNow = myDate.isSameDay(dueDate); 

        if (!dueNow) 
        return sectionHeader; 
        else return null; 
    } 

 And I get the message:

Save error: Invalid type:  Component.Apex.SectionHeader 

 

So I can't even run the example they give me!  It says that it's not valid...

 

Every time I take one step forward, I feel like I fall backwards a quarter mile.

stephanmstephanm

I assume that you've called support to enable the dynamic visualforce components pilot? Also that you're on the latest API version?

This was selected as the best answer
ScriptMonkeyScriptMonkey

Actually I hadn't realized I needed to until this past saturday night, after I posted this.  I'm still waiting for it to be enabled, but thank you for replying!

stephanmstephanm

I'd suggest calling support and specifically requesting that the request to enable it get escalated to "tier 3", who should be able to anble it for you.

JoshiJoshi

Change the return type of the function to Component.Apex.SectionHeader currently it is Component.Apex.PageMessage. This will make it work.