", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007T43nsSAB", "datePublished": "2018-11-11T17:52:21.000Z", "author": { "@type": "Person", "name": "Rajamohan Vakati", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000Cx9R2QAJ", "affiliation": { "@type": "Organization", "name": "Amazon" } } } ] } }
Skip to main content
Hi Guys,

I want to display a table of Account records with checkboxes in such a way that, whenever a checkbox is selected,it should display the related

Opportunities for the specific Account. Only one Account record must be selected at a time.

I was able to achieve similar functionality through Picklist, but how to do the same with checkboxes.

My Code for picklist is shown below

Controller

--------------

public class WrapperOpp {

   public List<OppWrapper> Opplist = new List<OppWrapper>();

   public String SelectedValue {get;set;}

    public List<SelectOption> Accs {get{

        List<SelectOption> AccName = new List<SelectOption>();

        for(Account a :[Select Id, name from Account limit 10])

        {

            AccName.add(new SelectOption(a.name,a.name));

        }

        return AccName;

      }

     }

     public PageReference refresh()

     {

       Opplist .clear();

       for(Account a :[Select id,name,(Select name from opportunities) from Account where name =:SelectedValue])

       {

         for (opportunity opp :a.opportunities)

         Opplist.add(new OppWrapper(false,opp));

       }

     return null;

     }

 public List<OppWrapper> getOppList()

     {

        System.debug('count'+Opplist.size());

        return Opplist;}

     public class OppWrapper{

    public Boolean selected{get;set;}

    public Opportunity opp{get;set;}

    public OppWrapper(Boolean selected1, Opportunity opp1)

    {

        selected = selected1;

        opp = opp1;

    }

}

}

VF page

-------------

<apex:page controller="WrapperOpp" >

  <apex:form >

    <apex:pageBlock >

     <apex:pageBlockSection >

            <apex:selectList value="{!SelectedValue}" size="1">

              <apex:selectOptions value="{!Accs}"/>

               <apex:actionSupport event="onchange" action="{!refresh}" reRender="OppTable"/>

            </apex:selectList>

            <apex:pageBlockTable value="{!Opplist}" id="OppTable" var="o">

            <apex:column value="{!o.opp.Name}"/>

            </apex:pageBlockTable>

     </apex:pageBlockSection>

    </apex:pageBlock>

   </apex:form>

</apex:page>

Thanks,

Abhilash 
1 answer
  1. Nov 11, 2018, 5:52 PM
    Try this code

     

    public class WrapperOpp {

    //Our collection of the class/wrapper objects wrapAccount

    public List<wrapAccount> wrapAccountList {get; set;}

    public List<Opportunity> selectedOpps{get;set;}

    public WrapperOpp(){

    if(wrapAccountList == null) {

    wrapAccountList = new List<wrapAccount>();

    for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {

    // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList

    wrapAccountList.add(new wrapAccount(a));

    }

    }

    }

    public void processSelected() {

    selectedOpps = new List<Opportunity>();

    Set<Id> accIds = new Set<Id>() ;

    for(wrapAccount wrapAccountObj : wrapAccountList) {

    if(wrapAccountObj.selected == true) {

    accIds.add(wrapAccountObj.acc.Id);

    }

    }

    System.debug('acc'+accIds);

    System.debug('asdasd'+[Select Id , Name , Amount,CloseDate from Opportunity where AccountId IN :accIds ]);

    selectedOpps.addAll([Select Id , Name , Amount,CloseDate from Opportunity where AccountId IN :accIds ]);

    }

    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value

    public class wrapAccount {

    public Account acc {get; set;}

    public Boolean selected {get; set;}

    //This is the contructor method. When we create a new wrapAccount object we pass a Account that is set to the acc property. We also set the selected value to false

    public wrapAccount(Account a) {

    acc = a;

    selected = false;

    }

    }

    }

     

    <apex:page controller="WrapperOpp" >

    <script type="text/javascript">

    function selectAllCheckboxes(obj,receivedInputID){

    var inputCheckBox = document.getElementsByTagName("input");

    for(var i=0; i<inputCheckBox.length; i++){

    if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){

    inputCheckBox[i].checked = obj.checked;

    }

    }

    }

    </script>

    <apex:form >

    <apex:pageBlock >

    <apex:pageBlockButtons >

    <apex:commandButton value="Show Selected Opportunities" action="{!processSelected}" rerender="table2"/>

    </apex:pageBlockButtons>

    <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">

    <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">

    <apex:column >

    <apex:facet name="header">

    <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>

    </apex:facet>

    <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>

    </apex:column>

    <apex:column value="{!accWrap.acc.Name}" />

    <apex:column value="{!accWrap.acc.BillingState}" />

    <apex:column value="{!accWrap.acc.Phone}" />

    </apex:pageBlockTable>

    <apex:pageBlockTable value="{!selectedOpps}" var="c" id="table2" title="Selected Opps">

    <apex:column value="{!c.Name}" headerValue=" Name"/>

    <apex:column value="{!c.Amount}" headerValue="Amount"/>

    <apex:column value="{!c.CloseDate}" headerValue="Close Date"/>

    </apex:pageBlockTable>

    </apex:pageblockSection>

    </apex:pageBlock>

    </apex:form>

    </apex:page>

     
0/9000