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
jaanvivekjaanvivek 

I am struggeling with ne requirement.

I am new to this tech, So i am doing practise by myself by taking help from bloga and community.

 

I am trying to develop a Vf page having questions with it's answers having checkbox as prefirx.

 

e.g.

 

Question1- who is prime minister of india.

                       a- dr. Man Mohan Singh

                        b- Narendra Modi

                        c- Nitessh Kumar

                        d- lalu Yadav

 

Same Questin 2

 

 

I have created Questions and options as custom bjects in my org.

 

Could you please provide some help how to start working on it.

 

 

 

Thanks,

JaanVivek

                 

Best Answer chosen by Admin (Salesforce Developers) 
AdrianCCAdrianCC

See the layout attribute on the apex:selectcheckboxes. The value should be "pageDirection".

All Answers

AdrianCCAdrianCC

Hello Jaan! and welcome :)

 

I think you'll need another custom object: Answer. This is going to link Question and Option(Lookup fields). You'll probably want a lookup to User and a date field for completion date.

 

Depending on how you want to scale your app a Survey and a Survey Answers custom objects might be useful to group up questions and answers. Question will have a lookup to Survey, Answer will have a lookup to Survey Answers. Also the lookup to User and the Completion Date will be moved to the Survey Answers object.

 

For the vf page I recommend using a wrapper class that puts togheter a pair of Question- Answer and also has a List<Option>. To show the list of wrappers in the page use apex:repeat. If you want me I can go into more detail.

 

 

Another option would be SurveyForce. This is a free package and I think it has all that you need. I've used it and it's pretty cool. See here: http://appexchange.salesforce.com/listingDetail?listingId=a0N30000003I2gDEAS

 

Thanks,

Adrian

jaanvivekjaanvivek

Thank you So much Adrian.

 

 

I am new to this and have very bad hand with coding.

 

In my org I have created "Questions" and "Options" as both objects . Option has a loookup named Questions.

 

Could you pls provide some more details by giving example of one question with it options having Radio button or check box enabled.

 

 

So that based on that logic i will try to implement my scenario.

 

 

Thanks,

JaanVivek

AdrianCCAdrianCC
<apex:page controller="surveyController" >
	<apex:pageBlock>
		<apex:form>
			<apex:repeat value="{!wrapperList}" var="wrapper" id="theRepeat">
				<apex:outputText value="{!wrapper.question.Name}" />
				<apex:selectCheckboxes value="{!wrapper.selectedOptions}">
		            <apex:selectOptions value="{!wrapper.options}"/>
		        </apex:selectCheckboxes>
		        <br />
			</apex:repeat>
		</apex:form>
	</apex:pageBlock>	
</apex:page>

 

public class surveyController {
	public List<Wrapper> wrapperList				{get; set;}

	public surveyController() {
		//here you'll have to have a way to retrieve a list of Questions and their related Option based on a criteria; you need to group them using something...
		List<Question__c> questionList = [SELECT Id, Name, (SELECT Id, Name FROM Options__r) FROM Question__c];//WHERE clause probably needed
		
		wrapperList = new List<Wrapper>();	
		for (Question__c question: questionList) {
			Wrapper wrapper = new Wrapper(question, question.Options__r);
			wrapperList.add(wrapper);
		}
	}

	public class Wrapper {
		public List<String> selectedOptions			{get; set;}//this will be a List of the Ids of the selected Options
		public List<SelectOption> options 			{get; set;}//a list of SelectOption that maps Option id(value) to Option name(label)	 

		public Question__c question 				{get; set;}

		public Wrapper(Question__c question, List<Option__c> optionsList) {
			this.question = question;
			this.selectedOptions = new List<String>();
			this.options = new List<SelectOption>();
			if ((optionsList == null) || (optionsList.size() == 0)) {
				options.add(new SelectOption('none','-- none --'));
			} else {
				for (Option__c option: optionsList) {
					options.add(new SelectOption(option.Id, option.Name));
				}
			}
		}
	}
}

 This example is not tested... I've written the code blindly, but it should work. It will show the question and its related options as checkboxes. It only shows information so you need to add a save function. For this you need to decide where and how to store the answers in the database. Also, you need a way to retrieve the Questions, some sort of criteria...

 

Thanks,

Adrian

 

 

jaanvivekjaanvivek

Thanks for Adrian Again..

I tried something based on your suggestions.

 

In my  scenario  I would like to explain the following points.

 

1- I have one custom object named Question__c having fields named QuestionName__cText Area(255), Assesment__cLookup(Assesment).

 

2- I have another object named Option__c having fields named  Option_Name__cText Area(255), Question__cLookup(Question).

 

here is my code

 

<apex:page standardController="Assesment__c" extensions="surveyController" sidebar="false" >
 <apex:pageBlock >
        <apex:form >
            <apex:repeat value="{!wrapperList}" var="wrapper" id="theRepeat">
                <apex:outputText value="{!wrapper.question.Name}" />
                <apex:selectCheckboxes value="{!wrapper.selectedOptions}">
                    <apex:selectOptions value="{!wrapper.options}"/>
                </apex:selectCheckboxes>
                <br />
            </apex:repeat>
        </apex:form>
    </apex:pageBlock>
</apex:page>

 

Apex Controller-

 

public class surveyController
{
     
     public List<Wrapper> wrapperList{get; set;}
 
 

    public surveyController(ApexPages.StandardController controller)
    {
        //here you'll have to have a way to retrieve a list of Questions and their related Option based on a criteria; you need to group them using something...
        List<Question__c> questionList = [SELECT Id, QuestionName__c, (SELECT Id, Option_Name__c FROM Options__r) FROM Question__c where Assesment__c=:ApexPages.currentPage().getParameters().get('id')];//WHERE clause probably needed
        
        wrapperList = new List<Wrapper>();  
        for (Question__c question: questionList)
        {
            Wrapper wrapper = new Wrapper(question, question.Options__r);
            wrapperList.add(wrapper);
        }
    
    
    }
    
    
    // wrapper class
    public class Wrapper
  {
        public List<String> selectedOptions{get; set;}//this will be a List of the Ids of the selected Options
        public List<SelectOption> options{get; set;}//a list of SelectOption that maps Option id(value) to Option name(label)     

        public Question__c question{get; set;}

        public Wrapper(Question__c question, List<Option__c> optionsList)
       {
            this.question = question;
            this.selectedOptions = new List<String>();
            this.options = new List<SelectOption>();
            if ((optionsList == null) || (optionsList.size() == 0))
            {
                options.add(new SelectOption('none','-- none --'));
            } 
            else
             {
                for (Option__c option: optionsList)
                {
                    options.add(new SelectOption(option.Id, option.Option_Name__c));
                }
            }
        }
    }
    

}

 

 

I am getting error

 

System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Question__c.Name

 


Could you please have  alook on to this.

 

 

Thanks,

JaanVivek

 

 
AdrianCCAdrianCC
<apex:page standardController="Assesment__c" extensions="surveyController" sidebar="false" >
 <apex:pageBlock >
        <apex:form >
            <apex:repeat value="{!wrapperList}" var="wrapper" id="theRepeat">
                <apex:outputText value="{!wrapper.question.QuestionName__c}" />
                <apex:selectCheckboxes value="{!wrapper.selectedOptions}">
                    <apex:selectOptions value="{!wrapper.options}"/>
                </apex:selectCheckboxes>
                <br />
            </apex:repeat>
        </apex:form>
    </apex:pageBlock>
</apex:page>

 

jaanvivekjaanvivek

It's awesome!!!!

 

But all options are coming in a single row, like

 

option1, option2, option3.

 

But if we need it to show in different rows. Like

 

Option 1,

Option 2,

Option3

 

What is your suggestion in this.

 

 

Thanks for your all time and valuable suggestions.

 

 

Thanks,

JaanVivek

 

AdrianCCAdrianCC

See the layout attribute on the apex:selectcheckboxes. The value should be "pageDirection".

This was selected as the best answer
jaanvivekjaanvivek

Thank you so much Adrian for your all time and valuable suggestions.

 

have a great day out.

 

 

 

Thanks,

JaanVivek

 

jaanvivekjaanvivek

Hi Adrain.

 

I am back with some small problem.

 

I want to show the "Questions" and it's "Options" in numbering. I meant to say.

 

Like

 

Question 1-   ...........

 

Options-

   a-

   b-

  c-

  d-

  .

  .

  .

  .

  .

Question10- .....

 

Options

    a-

    b-

   c-

  d-

 

Like this for all questions available in page. I my case I have 10 questions so i want to show Question 1 to Question 10

 

with options in the form of a,b,c,d.

 

 

Here is my VF Page Code-

 

<apex:page standardController="Assesment__c" extensions="surveyController" sidebar="false" >
<apex:sectionHeader title="Assesment- {!Assesment__c.AssesmentName__c} For {!Assesment__c.Course__r.CourseName__c}" />
        <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!wrapperList}" var="wrapper" id="theRepeat" >
          <h1>Question-</h1> <apex:outputText value="{!wrapper.question.QuestionName__c}" /><br/><br/>
          <h1>Options-</h1> <apex:selectCheckboxes value="{!wrapper.selectedOptions}" layout="pageDirection">
                <apex:selectOptions value="{!wrapper.options}"/> 
                </apex:selectCheckboxes>
                <br />  
            </apex:repeat>
           
          
            <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Submit" action="{!submit}" />
            </apex:pageBlockButtons>
     </apex:pageBlock>
      </apex:form>
</apex:page>

   Note- I tried to use followinh HTMl tags

 

<ol type="1"> <li> question</li></ol>

 

and <ol type="a"><li> options</li></ol>

 

 

But it works only for first question and very first option not for all Questions and Options.

 

 

Does any one can suggest something.

 

 

Thanks,

jaanVivek

jaanvivekjaanvivek

Could anyone suggest how do i proceed in this case.

 

In my scenarios I have 10 Questions and each question with 4 Options.

 

I just want to give them numbering format as i explained in below posts.

 

 

 

Thanks,

JaanVivek

AdrianCCAdrianCC

I guess there are several ways to do this...

 

One way would be like this:

    // wrapper class
    public class Wrapper
    {
public Integer questionNumber {get; set;} public List<String> selectedOptions{get; set;}//this will be a List of the Ids of the selected Options public List<SelectOption> options{get; set;}//a list of SelectOption that maps Option id(value) to Option name(label) public Question__c question{get; set;} public Wrapper(Question__c question, List<Option__c> optionsList, Integer qn) { String alphabet = 'abcdefgh....';
this.questionNumber = qn;
this.question = question; this.selectedOptions = new List<String>(); this.options = new List<SelectOption>(); if ((optionsList == null) || (optionsList.size() == 0)) { options.add(new SelectOption('none','-- none --')); } else { Integer i = 0; for (Option__c option: optionsList) { options.add(new SelectOption(option.Id, alphabet.substring(i, i + 1) + ' ' + option.Option_Name__c)); i++; } } } }

 

    public surveyController(ApexPages.StandardController controller)
    {
        //here you'll have to have a way to retrieve a list of Questions and their related Option based on a criteria; you need to group them using something...
        List<Question__c> questionList = [SELECT Id, QuestionName__c, (SELECT Id, Option_Name__c FROM Options__r) FROM Question__c where Assesment__c=:ApexPages.currentPage().getParameters().get('id')];//WHERE clause probably needed
        
        wrapperList = new List<Wrapper>(); 
        Integer i = 0; 
        for (Question__c question: questionList)
        {
            Wrapper wrapper = new Wrapper(question, question.Options__r, i++);
            wrapperList.add(wrapper);
        }
    
    
    }

 

<apex:page standardController="Assesment__c" extensions="surveyController" sidebar="false" >
<apex:sectionHeader title="Assesment- {!Assesment__c.AssesmentName__c} For {!Assesment__c.Course__r.CourseName__c}" />
        <apex:form >
        <apex:pageBlock >
            <apex:repeat value="{!wrapperList}" var="wrapper" id="theRepeat" >
          <h1>Question-{!wrapper.questionNumber}</h1> <apex:outputText value="{!wrapper.question.QuestionName__c}" /><br/><br/>
          <h1>Options-</h1> <apex:selectCheckboxes value="{!wrapper.selectedOptions}" layout="pageDirection">
                <apex:selectOptions value="{!wrapper.options}"/> 
                </apex:selectCheckboxes>
                <br />  
            </apex:repeat>
           
          
            <apex:pageBlockButtons location="bottom">
            <apex:commandButton value="Submit" action="{!submit}" />
            </apex:pageBlockButtons>
     </apex:pageBlock>
      </apex:form>
</apex:page>

 

 

 

 

 

 

jaanvivekjaanvivek

Thnks It's working fine.