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
Jose María Nicolás ArfelisJose María Nicolás Arfelis 

Showing the corresponding Answer field according to the data type in the Question

Hi everybody,

this thread could help other Salesforce Community followers if they are facing the same situation.

Our client requires a sort of dynamic questions for Student Applications via Web forms.
That means, for every academic programm there will be questions the applicant will have to answer.
For example, if the applicant chooses the academic programm "Medicine" on the web form, let's assume the following questions would appear:

1. Have you ever performed complex surgical operations? (Answer Type: Yes or No)
2. If yes, which was your most complex surgical operation? (Answer Type: Text)
3. When did you begin to perform operations? (Answer Type: Date)

That means, according to the selected academic programm, the corresponding questions will appear. And according to the type of question (if picklist, text, Yes/No, numeric, date, multiple choice) the corresponding type of Answer will appear.


I built a custom object called Question with fields Question ID, ID of the academic programm, Type (if picklist, if checkbox, if text, if date, if numeric).
I also built a custom object called Answers with fields Question ID, Application ID, Type (text, numeric, Date, boolean), Answer Text (type text), Answer Numeric (type numeric) , Answer Boolean (type boolean), Answer date (type date).

The idea in Salesforce is that when the question is numeric, the Answer field which should appear should be the Answer numeric, if the question is date type, Answer date should appear, if the question is text or checkbox type, Answer text should appear.

Question ID in the custom object Answer is a lookup to the object Question.

Is there any form to manage this through validation or Visualforce?.
Best Answer chosen by Jose María Nicolás Arfelis
sathishkumar periyasamysathishkumar periyasamy
Hello Jose,  You have to build force.com site to establish webforce and need to build VF page to acombalish your requirement. As per your example -- You can create a new picklist field called "Answer Type" on the Question object. Picklist value should be "Text, Check Box,Radio Button,  Text Area". Basesd on Question and Answer Type - VF page should be dynamically display.

All Answers

VineetKumarVineetKumar
  • You can either create multiple input fields and render them based on the type of the fields from your custom objects (Follow this if data sits in different objects after your save).
  • If you have the data to be saved onto the same object, you can use the <apex:inputField> tag and bind the variables to the <object.field> instance, salesforce will handle the rendering as per the field type. Your page will look something like this below
<SObject> dummyObject = new <SObject>();
<apex:pageBlockSection title="Dynamic fields" columns="1" showHeader="true">
<apex:repeat value="{!fieldNames}" var="FieldName" >
<apex:pageBlockSectionItem >
<apex:outputLabel value="{!FieldName}" />
<apex:inputField value="{!object[FieldName]}" />
</apex:pageBlockSectionItem>
</apex:repeat>
</apex:pageBlockSection>
Jose María Nicolás ArfelisJose María Nicolás Arfelis
Hi Vineet, thanks for your answer. I guess your suggested code would apply a site or Apex site or part of an apex site for the web site where the Application form will be displayed.
In my case it is only about the configuration in the Salesforce application, I am not programming the web site, my part is just to configure the mapping in Salesforce.
 
VineetKumarVineetKumar
I'm afraid my friend, but this requirement cannot be done just by configuration, you need to develop a Visualforce page to handle this dynamic nature.

If my suggestion(s) worked, do let me know by marking the answer as "Best Answer" right under the comment.
This will help the rest of the community should they have a similar issue in the future. 

Thank you..
sathishkumar periyasamysathishkumar periyasamy
Hello Jose,  You have to build force.com site to establish webforce and need to build VF page to acombalish your requirement. As per your example -- You can create a new picklist field called "Answer Type" on the Question object. Picklist value should be "Text, Check Box,Radio Button,  Text Area". Basesd on Question and Answer Type - VF page should be dynamically display.
This was selected as the best answer
Jose María Nicolás ArfelisJose María Nicolás Arfelis
Thanks guys for your answers. Unfortunately I am not a programmer, only a functional analyst. Anyway, I know now, that this can only be by using code.
VineetKumarVineetKumar
Happy to help. If my suggestion(s) worked, do mark it as the answer as "Best Answer" right under the comment.
This will help the rest of the community should they have a similar issue in the future. 

Thank you..
Jose María Nicolás ArfelisJose María Nicolás Arfelis
Thanks Vineet and Sathish.