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
DylbertDylbert 

Using a Select All Button in Apex and Visualforce

Hi All,

I got nice little Opportunity Product application from Michaelforce which I've modified to suit our business and it's been fine till now.  Our Sales Reps have requested the ability to be able to add multiple search result lines to the "shopping cart" when adding products with a "Select All" button.

I have the following codes below, but my coding experience is minimal. 

Class Snippet:
public void addToShoppingCart(){
    
        // This function runs when a user hits "select" button next to a product
    
        for(PricebookEntry d : AvailableProducts){
            if((String)d.Id==toSelect){
                shoppingCart.add(new opportunityLineItem(OpportunityId=theOpp.Id, PriceBookEntry=d, PriceBookEntryId=d.Id, UnitPrice=d.UnitPrice));
                break;
            }
        }
        
        updateAvailableList();  
    }
    
    public void addAllToShoppingCart(){
    
        // This function runs when a user hits "add all" button
    
        for(PricebookEntry d : AvailableProducts){
                shoppingCart.add(new opportunityLineItem(OpportunityId=theOpp.Id, PriceBookEntry=d, PriceBookEntryId=d.Id, UnitPrice=d.UnitPrice));
        }
        
        updateAvailableList();  
    }

I was advised to add the "add all" method by a Java developer at our company.

VF Page Snippet:
<apex:column>
                            <apex:facet name="header"><apex:CommandButton value="Select All" action="{!addToShoppingCart}" reRender="selected,searchResults" immediate="true">
                                </apex:commandButton>
                            </apex:facet>                                                             
                            <!-- command button in a column... neato -->
                            <apex:CommandButton value="Select" action="{!addToShoppingCart}" reRender="selected,searchResults" immediate="true">
                                <!-- again we use apex:param to be able to tell the controller which row we are working with -->
                                <apex:param value="{!a.Id}" assignTo="{!toSelect}" name="toSelect"/>
                            </apex:commandButton>
                        </apex:column>

I can't seem to get the button to add any lines, I'm not sure if I'm missing apex:param values liek on the other standard select buttons, and if so, what would I use as a value to select all search result lines?

I know I can go the route of using checkboxes and wrappers to do this, but it woul dbe nice if it could be done with the button.

If anyone has any suggestions or can point me in the right direction, it would be appreciated.
Best Answer chosen by Dylbert
Ansh CoderAnsh Coder
Hi,
Use this code.
you used the same action for both commandbuttons.
change the action for 'select all' command button to 'addAllToShoppingCart'.
<apex:column>
                            <apex:facet name="header"><apex:CommandButton value="Select All" action="{!addAllToShoppingCart}" reRender="selected,searchResults" immediate="true">
                                </apex:commandButton>
                            </apex:facet>                                                             
                            <!-- command button in a column... neato -->
                            <apex:CommandButton value="Select" action="{!addToShoppingCart}" reRender="selected,searchResults" immediate="true">
                                <!-- again we use apex:param to be able to tell the controller which row we are working with -->
                                <apex:param value="{!a.Id}" assignTo="{!toSelect}" name="toSelect"/>
                            </apex:commandButton>
                        </apex:column>

It will work.

Thanks,
Anand Sharma

All Answers

Ansh CoderAnsh Coder
Hi,
Use this code.
you used the same action for both commandbuttons.
change the action for 'select all' command button to 'addAllToShoppingCart'.
<apex:column>
                            <apex:facet name="header"><apex:CommandButton value="Select All" action="{!addAllToShoppingCart}" reRender="selected,searchResults" immediate="true">
                                </apex:commandButton>
                            </apex:facet>                                                             
                            <!-- command button in a column... neato -->
                            <apex:CommandButton value="Select" action="{!addToShoppingCart}" reRender="selected,searchResults" immediate="true">
                                <!-- again we use apex:param to be able to tell the controller which row we are working with -->
                                <apex:param value="{!a.Id}" assignTo="{!toSelect}" name="toSelect"/>
                            </apex:commandButton>
                        </apex:column>

It will work.

Thanks,
Anand Sharma
This was selected as the best answer
DylbertDylbert
Hi Anand,

Thanks so much for that, it got it working!