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
anuj kumar 98anuj kumar 98 

retrieving the record based on selection of a checkbox

I need to retrieve  the records ( All opportunities with Closed won )  on selection of a checkbox (inputcheckbox)  on click of SEARCH command button on the page.
 
after searching around, all I have seen are references to using a wrapper class, which seems a little bit overboard for what I am looking to do. I am beginner in developer role. I am using the below code..

public class inputcheckbox{

    public boolean mycheckval { get; set; }
    public inputcheckbox() {
    mycheckval = false;
    }
    List<Opportunity> Opr;
    public List<Opportunity> getopr(){
        return opr;
    }
        public void closedWon(){
        if(mycheckval) {
        
        opr= [select name, stagename, closedate, expectedrevenue, probability from Opportunity where stagename= 'closed won'];
        
        //return null;
        }
    }   
        
}
VF:---

apex:page controller="inputcheckbox" sidebar="false" showHeader="false">
<apex:form >
            <apex:pageblock title="Search opportunity by stage"> 
        
            <apex:inputcheckbox value="{!mycheckval}" />
     
                  
            <apex:commandButton value="search" action="{!closedwon}"/>
            
            <apex:pageBlockTable value="{!opr}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.closedate}"/>
                <apex:column value="{!a.expectedrevenue}"/>
                <apex:column value="{!a.probability}"/>
        </apex:pageBlockTable>
        
        
  
        </apex:pageblock>
</apex:form>
</apex:page>


i am able to retriev whether the check box selected or not but not able to get it's value dynamically. i have hadcoded the stagename in SOQL and hence it's working based on selection of check box.. i need to have stagename dynamically based of selection of check box
Best Answer chosen by anuj kumar 98
Rishab TyagiRishab Tyagi
Hello Anuj,

Like you said that you are new to the developer role so let's not mess up various languages and stick to one for now. With that said I am about to post two different variations of the code out of which, I believe that, at least one will resolve your question.

Scenario #1: You want data from multiple stages at once, eg. You want to get data of both Closed Won and Prospecting stage at once in some cases. In this case, I would recommend using the current checkbox strategy and proceed with this code.

Controller:
public class inputcheckbox{
    public boolean closedWon { get; set; }
    public boolean prospecting { get; set; }
    public inputcheckbox() {
        closedWon = false;
        prospecting = false;
    }
    List<Opportunity> Opr;
    public List<Opportunity> getopr(){
        return opr;
    }
    public void closedWon(){
        list<string> stages = new list<string>();
        if(closedWon){
            stages.add('Closed Won');
        }
        if(prospecting){
            stages.add('Prospecting');
        }
        if(closedWon || prospecting) {
            opr= [select name, stagename, closedate, expectedrevenue, probability from Opportunity where stagename in :stages];
        }
    }
}
VF:
<apex:page controller="inputcheckbox" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="Search opportunity by stage"> 
            <apex:inputcheckbox value="{!closedWon}"  />
            <label>Closed Won</label>   &nbsp;
            <apex:inputcheckbox value="{!Prospecting}"/>
            <label>Prospecting</label>   &nbsp;
            <apex:commandButton value="search" action="{!closedwon}"/>
            <apex:pageBlockTable value="{!opr}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.closedate}"/>
                <apex:column value="{!a.expectedrevenue}"/>
                <apex:column value="{!a.probability}"/>
            </apex:pageBlockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>


Scenario #2: You always need to select only one stage and will query data from that stage only. That means if you click on prospecting then Closed Won will be unselected. In this case, I would recommend dropping checkboxes altogether and using the radio buttons.

Controller:
public class inputcheckbox{
    public string stage { get; set;}
    public inputcheckbox() {

    }
    List<Opportunity> Opr;
    public List<Opportunity> getopr(){
        return opr;
    }
    public void closedWon(){
        opr= [select name, stagename, closedate, expectedrevenue, probability from Opportunity where stagename = :stage];
    }
    public list<selectOption> getStages(){
        list<selectOption> options = new list<selectoption>();
        for(schema.PicklistEntry ple: Opportunity.stageName.getDescribe().getPicklistValues()){
            if(ple.getValue() == 'Closed Won' || ple.getValue() == 'Prospecting')
            	options.add(new selectOption(ple.getValue(), ple.getLabel()));
        }
        return options;
    }
}
VF:
<apex:page controller="inputcheckbox" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="Search opportunity by stage"> 
            <apex:selectRadio value="{!stage}">
                <apex:selectOptions value="{!stages}"/>
            </apex:selectRadio>
            <apex:commandButton value="search" action="{!closedwon}"/>
            <apex:pageBlockTable value="{!opr}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.closedate}"/>
                <apex:column value="{!a.expectedrevenue}"/>
                <apex:column value="{!a.probability}"/>
            </apex:pageBlockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

In the second scenario, you may add the stages in the if part of the getStages function, and if you want all stages to be displayed then you may remove the if line completely.
 

All Answers

Rishab TyagiRishab Tyagi
Hello Anuj,

You have taken the correct approach. Now, you need to do two things in order to dynamically query records:

1. Add a public string to the class like your boolean variable and then where you have given the stagename as 'closed won' in the query, there you have to give the string that you have created in this step with a colon ':', in front of the string.

2. Create a select list which will show all the picklist values of stage field in opportunity, using apex:selectlist attribute, and then map it with the string that you have created in the previous step.
anuj kumar 98anuj kumar 98
Thanks Rishabh for your reply!
I am not using drop down list which i need populate with stagename. I am using just 2 inputcheckboxes (Closed won, Open). I tried the first option but was not able to retieve the value of checkbox that i selected for querying. I agree, i will definately use the string variable somewhere to get  value and for querying, bt not sure how and where to use that.

For example: If i check on Closed won, i need to see all opportunities in table with this stagename. that's is my requirement,
Rishab TyagiRishab Tyagi
I am getting a rough idea of what you are trying to do here. Let's say that you have 2 cases one is query all the opportunity records that are closed won and in the second you want to query the opportunities which are not closed won.

If this is the case then you can write another if else where you can query the other records using the same query with stagename != 'Closed Won'.

Another scenario that I understand from your response is that you want to create checkbox for each stage, eg. Closed Won, Prospecting, etc.

In this case, you will need to use a bit of jQuery, or javascript whichever you are comfortable with. Whenever one checkbox is clicked execute one function which will uncheck all the other checkboxes. Then use an input hidden type of field having value as your string in the controller. Place the value corresponding to the checkbox in the input hidden field. As for setting which value corresponds to which checkbox, you may use data-value or any other type of data-* attribute and then fetch the value in jQuery.

Tell me if that helps. Or, if I have misunderstood something then you may post a screenshot of the same explaining things in order for me to better visualize of what you are trying to achieve.
anuj kumar 98anuj kumar 98
Hi Rishabh, The second scenario is right! If i select check box for Closed Won, i should be able to see all opportunities with this stage table. Please see code that i am using for reference. can you please help with the code, will try to understand the logic. I am still new to Java script.

Controller:-

public class inputcheckbox{
    public boolean mycheckval { get; set; }
    public inputcheckbox() {
    mycheckval = false;
    }
    List<Opportunity> Opr;
    public List<Opportunity> getopr(){
        return opr;
    }
        public void closedWon(){
        if(mycheckval) {
        opr= [select name, stagename, closedate, expectedrevenue, probability from Opportunity where stagename= 'closed won'];
        //return null;
        }
    }   
        
}

VF:-

<apex:page controller="inputcheckbox" sidebar="false" showHeader="false">
<apex:form >
            <apex:pageblock title="Search opportunity by stage"> 
            <apex:inputcheckbox value="{!mycheckval}"  />
            <label>Closed Won</label>   &nbsp;
            <apex:inputcheckbox />
            <label>Prospecting</label>   &nbsp;
            <apex:commandButton value="search" action="{!closedwon}"/>
            <apex:pageBlockTable value="{!opr}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.closedate}"/>
                <apex:column value="{!a.expectedrevenue}"/>
                <apex:column value="{!a.probability}"/>
            </apex:pageBlockTable>
</apex:pageblock>
</apex:form>
</apex:page>
Rishab TyagiRishab Tyagi
Hello Anuj,

Like you said that you are new to the developer role so let's not mess up various languages and stick to one for now. With that said I am about to post two different variations of the code out of which, I believe that, at least one will resolve your question.

Scenario #1: You want data from multiple stages at once, eg. You want to get data of both Closed Won and Prospecting stage at once in some cases. In this case, I would recommend using the current checkbox strategy and proceed with this code.

Controller:
public class inputcheckbox{
    public boolean closedWon { get; set; }
    public boolean prospecting { get; set; }
    public inputcheckbox() {
        closedWon = false;
        prospecting = false;
    }
    List<Opportunity> Opr;
    public List<Opportunity> getopr(){
        return opr;
    }
    public void closedWon(){
        list<string> stages = new list<string>();
        if(closedWon){
            stages.add('Closed Won');
        }
        if(prospecting){
            stages.add('Prospecting');
        }
        if(closedWon || prospecting) {
            opr= [select name, stagename, closedate, expectedrevenue, probability from Opportunity where stagename in :stages];
        }
    }
}
VF:
<apex:page controller="inputcheckbox" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="Search opportunity by stage"> 
            <apex:inputcheckbox value="{!closedWon}"  />
            <label>Closed Won</label>   &nbsp;
            <apex:inputcheckbox value="{!Prospecting}"/>
            <label>Prospecting</label>   &nbsp;
            <apex:commandButton value="search" action="{!closedwon}"/>
            <apex:pageBlockTable value="{!opr}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.closedate}"/>
                <apex:column value="{!a.expectedrevenue}"/>
                <apex:column value="{!a.probability}"/>
            </apex:pageBlockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>


Scenario #2: You always need to select only one stage and will query data from that stage only. That means if you click on prospecting then Closed Won will be unselected. In this case, I would recommend dropping checkboxes altogether and using the radio buttons.

Controller:
public class inputcheckbox{
    public string stage { get; set;}
    public inputcheckbox() {

    }
    List<Opportunity> Opr;
    public List<Opportunity> getopr(){
        return opr;
    }
    public void closedWon(){
        opr= [select name, stagename, closedate, expectedrevenue, probability from Opportunity where stagename = :stage];
    }
    public list<selectOption> getStages(){
        list<selectOption> options = new list<selectoption>();
        for(schema.PicklistEntry ple: Opportunity.stageName.getDescribe().getPicklistValues()){
            if(ple.getValue() == 'Closed Won' || ple.getValue() == 'Prospecting')
            	options.add(new selectOption(ple.getValue(), ple.getLabel()));
        }
        return options;
    }
}
VF:
<apex:page controller="inputcheckbox" sidebar="false" showHeader="false">
    <apex:form >
        <apex:pageblock title="Search opportunity by stage"> 
            <apex:selectRadio value="{!stage}">
                <apex:selectOptions value="{!stages}"/>
            </apex:selectRadio>
            <apex:commandButton value="search" action="{!closedwon}"/>
            <apex:pageBlockTable value="{!opr}" var="a">
                <apex:column value="{!a.Name}"/>
                <apex:column value="{!a.stagename}"/>
                <apex:column value="{!a.closedate}"/>
                <apex:column value="{!a.expectedrevenue}"/>
                <apex:column value="{!a.probability}"/>
            </apex:pageBlockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

In the second scenario, you may add the stages in the if part of the getStages function, and if you want all stages to be displayed then you may remove the if line completely.
 
This was selected as the best answer
anuj kumar 98anuj kumar 98
Thanks much Rishabh for your help with this!  I was able to understand the code too!