0

I am using Visualforce to list out a few different opportunities a checkbox for each one. My goal is to select an opportunity by clicking the checkbox, then pressing a commandbutton to pass that opportunity info into apex. I am setting the value field of the checkbox to an opportunity object in my apex controller but it is trying to return a boolean instead of the opportunity object. Here is a snippet of my VF and Apex:

VF:

<apex:page standardController="System__c" extensions="SystemAddToOp" recordSetVar="system__c" >    
<apex:form >
    <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlock title="Add These Systems to an Opportunity">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!updateSystems}" value="Add Selected Systems to Opportunity" />
        </apex:pageBlockButtons>
        <apex:pageBlockTable value="{!ops}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column value="{!o.id}"/>
            <apex:column value="{!o.CloseDate}"/>
            <apex:column value="{!o.Amount}"/>
            <apex:column headerValue="Select">
                <apex:inputCheckbox value="{!selectedOp}"/>
            </apex:column>
        </apex:pageBlockTable> 
    </apex:pageBlock>
</apex:form>

APEX (snippet):

public Opportunity selectedOp{get;set;}
public PageReference updateSystems()
{       
    insert selectedOp;
    system.debug(selectedOp);

    return null;
}

Any guidance here would be much appreciated. Thanks!

1

The first thing you should know is that you can't have more than 1 input set to the same value. Otherwise itll always be the value of the last input.

<apex:inputCheckbox value="{!selectedOp}"/>

With that, the only check box that will work is the last check box on the page. If this was a field on the opp you would be fine but there's a better way (see below).

Second thing you should know is check boxes are boolean so you'll only ever get true/false, you'll never get any opp specific information this way.

What you need to do is create a wrapper class:

public class oppWrapper {
    public Opportunity obj { get; set; }
    public Boolean selected { get; set; }

    public oppWrapper(Opportunity opp) {
        this.obj = opp;
        this.selected = false;
    }
}

Then create a list of this object:

List<OppWrapper> wrappedOpps = new List<OppWrapper>();

for (Opportunity opp : opportunities) {
    wrappedOpps.add(new oppWrapper(opp));
}

Display this onto your page:

<apex:pageBlockTable value="{!wrappedOpps}" var="opp">
    <apex:column value="{!opp.selected}"/>
    <apex:column value="{!opp.obj.Name}"/>
</apex:pageBlockTable>

Lastly, your command button will loop through this list to find the selected opp(s)

public void action() {
    List<Opportunity> selectedOps = new List<Opportunity>();
    for (oppWrapper ow : wrappedOpps) {
        if (ow.selected) {
            selectedOps.add(ow.obj);
        }
    }

    // Do what ever you want with your selected opps
}

I made a component a while back (pretty old so don't judge me on the code quality) that does a good job at creating a list of records and wrapping them with a checkbox. It also uses the standard set controller and provides some pagination, sorting and filtering options IIRC.

  • Thank you for the help! This is exactly what I was looking to do. – John Jul 24 '18 at 1:55

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.