0

I am trying to display a program name and a list of seminars connected to the program in a select list box in a vf page. I need to do it 5 times (5 fields) showing the same values so a student can choose up to 5 seminars if there are that many in a program.

I am getting an error when passing the two fields to the VF page (seminarItem and programName).

  1. What do I need to go to get the code to work?
  2. Is there an intelligent way to generate the same field 5 times but each field will be "mapped" to a separate field in the SF Object which means there is a field named seminar_1, seminar_2 and so on that need the seminar that the student chooses. Or should I just repeat the same apex:outputField 5 times.

Here is the VF:

<apex:page controller="vfCnt_chooseSeminar">  <apex:form >   <apex:pageMessages />
        <apex:pageBlock title="Choose Seminar(s) for " mode="edit">
         <apex:pageBlockSection title="{!programName}"  columns="2">                                
                <apex:outputField label="Seminar Name:" value="{!seminarItem}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>     </apex:page>

Here is the controller:

public class vfCnt_chooseSeminar {

    private string programID = ''; 
    public List<Program__c> seminarItem{get;set;} 
    public String programName{get;set;}


    public vfCnt_chooseSeminar()
    {
        //Constructor
        List<Program__c> seminarList = new List<Program__c>();
        programID = System.currentPageReference().getParameters().get('pId');
        seminarList = [Select Seminar1__c,Seminar2__c,Seminar3__c,Seminar4__c,Seminar5__c,ID,Name From Program__c Where ID = :programID];
        Set <String> sems = New Set <String>();


        if(seminarList.isEmpty() == false)
        { 
            for(Program__c record :seminarList) 
               { 
                    sems.add((String) record.get('Seminar1__c')); 
                    sems.add((String) record.get('Seminar2__c')); 
                    sems.add((String) record.get('Seminar3__c')); 
                    sems.add((String) record.get('Seminar4__c')); 
                    sems.add((String) record.get('Seminar5__c')); 
                } 
//           seminarItem = seminarList;
            seminarItem = sems;
           programName = seminarList[];



        }
        else
        { 
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'The Program ID is invalid.');
            ApexPages.addMessage(msg);
        }
    } 


}
2

This may be what you are looking for:

public class vfCnt_chooseSeminar {

    public String[] fields {get; private set;}
    public Program__c seminar {get; private set;}

    public vfCnt_chooseSeminar() {

        fields = new String[] {
                'Seminar1__c',
                'Seminar2__c',
                'Seminar3__c',
                'Seminar4__c',
                'Seminar5__c'
                };

        String programID = System.currentPageReference().getParameters().get('pId');
        List<Program__c> seminars = [
                Select Seminar1__c,Seminar2__c,Seminar3__c,Seminar4__c,Seminar5__c,ID,Name
                From Program__c
                Where ID = :programID
                ];
        if (!seminars.isEmpty()) {
            seminar = seminars[0];
        } else {
            ApexPages.addMessage(new ApexPages.Message(
                    ApexPages.Severity.ERROR,
                    'The Program ID is invalid.'
                    ));
        }
    }
}

and then the Visualforce can use the map-like syntax to display each field using its name:

<apex:pageBlockSection title="{!seminar.Name}" columns="2">
    <apex:repeat value="{!fields}" var="f">                               
        <apex:outputField label="Seminar Name:" value="{!seminar[f]}"/>
    </apex:repeat>
</apex:pageBlockSection>

However, unless you want the number of fields shown to vary (in which case you would also have to use dynamic SOQL to query the correct fields), you might as well just hard code the field names:

<apex:pageBlockSection title="{!seminar.Name}" columns="2">
    <apex:outputField label="Seminar Name:" value="{!seminar.Seminar1__c}"/>
    <apex:outputField label="Seminar Name:" value="{!seminar.Seminar2__c}"/>
    <apex:outputField label="Seminar Name:" value="{!seminar.Seminar3__c}"/>
    <apex:outputField label="Seminar Name:" value="{!seminar.Seminar4__c}"/>
    <apex:outputField label="Seminar Name:" value="{!seminar.Seminar5__c}"/>
</apex:pageBlockSection>

PS

To allow these to be selectable you will need some separate Boolean fields. The simplest way to do that would be:

public Boolean selected1 {get; set;}
public Boolean selected2 {get; set;}
...

<apex:pageBlockSection title="{!seminar.Name}" columns="2">
    <apex:pageBlockSectionItem>
        <apex:outputLabel value="Seminar Name: {!seminar.Seminar1__c}"/>
        <apex:inputCheckbox value="{!selected1}"/>
    </apex:pageBlockSectionItem>
    <apex:pageBlockSectionItem>
        <apex:outputLabel value="Seminar Name: {!seminar.Seminar2__c}"/>
        <apex:inputCheckbox value="{!selected2}"/>
    </apex:pageBlockSectionItem>
    ...
</apex:pageBlockSection>
  • Many thanks. You most certainly helped me but I need all seminars from the query listed in 5 different select fields (drop down fields) so the user can choose 5 seminars of their choice send it back to SF. Believe me, I'd be happy to do this myself but I have been trying to understand how to do this but no go. How to get it in select fields? – Apex N-u-b Apr 4 '16 at 9:12
  • @ApexN-u-b See the PS for one way. – Keith C Apr 4 '16 at 9:47
  • 1) The PS put it in a checkbox, I need a drop down (like picklist). 2) Also, it only shows the recordID, not the title text as it does in your first version. Why is that? I tried something else and also just get the recordid. How to get the name to display? Thanks for going the extra mile. – Apex N-u-b Apr 4 '16 at 10:08
  • @ApexN-u-b OK I'm obviously not following what you need. You might consider deleting this question and describing exactly the UI you are looking for (and include your current code) in a new question. If Seminar1__c is an Id value using {! Seminar1__r.Name} (and including same in query) should give you the name. – Keith C Apr 4 '16 at 10:44
  • Excellent. {! Seminar1__r.Name} is what I needed. Many thanks Keith. – Apex N-u-b Apr 4 '16 at 10:59

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.