0

I have an AppSettingsController and a VisualForce page and on the page the user enters config settings into the app. Things like a username and password and a web service URL. The web service retrieves data and this data is added to Salesforce as leads.

I want them to set a campaign on the VF page so that the data retrieved from the web service is also added to a specific campaign.

Right now I have added a select list to the page, and in my controller I have added a field to the class, I am assigning the field values but nothing is displayed in the view.

Here is the required info from the page

<apex:page Controller="AppSettingController" docType="html-5.0">
    <apex:messages />
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Settings" columns="1">
                <apex:pageblockSectionItem >
                    <label>Campaign</label>
                    <apex:selectlist>
                        <apex:selectoptions value="{!campaigns}"/>
                    </apex:selectlist>
                </apex:pageblockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>     
</apex:page>

And in my controller:

public with sharing class AppSettingController
{

    public List<String> campaigns;

    public AppSettingController()
    {
        setView();    
    }

    public void setView()
    {
         campaigns = new List<String>();
         campaigns.add('Campaign test');
    }
}

When the page is rendered, the selectlist is just an empty control.

1

This is an example of how your controller should be modified to reflect the values.

public with sharing class AppSettingController {

        public List<SelectOption> campaigns = new List<SelectOption>();

        public AppSettingController() {
            setView();    
        }

        public void setView() {

             campaigns.add(new SelectOption('Campaign Value', 'Campaign Label'));
             // add other items

        }


        public List<SelectOption> getCampaigns() {
            return campaigns;
        }
}
1

Your value binding for an <apex:selectOptions> needs to return a List<SelectOption> rather than a string array.

Additionally, you need to provide a getter method to make the binding available to Visualforce, which you can do synthetically by adding { get; set; } following the definition of a public instance variable.

The reason for using SelectOption rather than String is that your options typically need to store a display value or label (in your case, the Campaign name) and an internal API value (likely the Campaign Id). You'll pass both values to the SelectOption constructor, and the value of the select list element itself will be the API value of the selected SelectOption.

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.