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
Alexander AtkinsonAlexander Atkinson 

Visual Force Page: Button to submit table items that are checked (Checkbox)

Hello, I have a Visual Force Page that displays a table of information / records. Each row has a checkbox to represent if it has been selected or not.
There is a button called "Quote Selected".

My goal here is when this button is pressed, all the items on the table that are selected have their information / ids passed to a list in another Apex controller that can be used on my 2nd VFP to generate a quote.

VFP:
<apex:form>
            <table width="100%" class="data-table">
                <tr class="headerRow">
                    <th>Select</th>
                    <th>SUPPLIER</th>
                    <th>DAY RATE</th>
                    <th>STANDING CHARGE</th>
                    <th>TERM</th>
                    <th>ANNUAL CHARGE</th>
                    <th>TOTAL AMOUNT</th>
                    <th>EXTRA INFORMATION</th>
                </tr>
                <apex:repeat value="{!data}" var="Results">
                    <tr>
                        <td>{!Results}<input type="checkbox" value="{!Results}"/></td>
                     	<td><apex:outputField value="{!Results.Supplier__c}"/></td>
                     	<td><apex:outputField value="{!Results.Day_Rate__c}"/></td>
                     	<td><apex:outputField value="{!Results.Standing_Charge__c}"/></td>
                     	<td><apex:outputField value="{!Results.Term__c}"/></td>
                     	<td><apex:outputField value="{!Results.Annual_Charge__c}"/></td>
                     	<td><apex:outputField value="{!Results.Total_Amount__c}"/></td>   
                     	<td><apex:outputField value="{!Results.Extra_Info__c}"/></td>
		    </tr>
                </apex:repeat>
		</table>

		<apex:commandButton action="{!quoteSelected}" value="Quote Selected"/>
        </apex:form>

 
Sampath SuranjiSampath Suranji
Hi,

You can use a list to contains selected Ids and you can update the list when user check in each check box using action function,
Please check below example which used Account object
<apex:page controller="devForum10_22" >
    
    
    <apex:form>
        
        <table>
            
            <tr class="headerRow">
                <th>Check</th>
                <th>Id</th>
                <th>Name</th>
            </tr>
            <apex:repeat value="{!AccountList}" var="r">
                <tr>
                    
                    <td><input type="checkbox" value="{!chkBoxValue}" onChange="quotesCheck('{!r.id}', this.checked);"/></td>
                    <td><apex:outputField value="{!r.id}"/></td>
                    <td><apex:outputField value="{!r.name}"/></td>
                    
                </tr>
            </apex:repeat>
        </table>
        
        <apex:commandButton action="{!quoteSelected}" value="Quote Selected"/>
        
        <apex:actionFunction action="{!addQuotesToChecked}" name="quotesCheck" reRender="sasa">
            <apex:param name="quoteId" value=""/> 
            <apex:param name="chkValue" value=""/>
        </apex:actionFunction>
    </apex:form>
</apex:page>
 
public class devForum10_22 {

    public List<Account>AccountList {get;set;}
    public List<Id>checkedQuotes {get;set;}
    public boolean chkBoxValue{get;set;}
    
    public devForum10_22(){
        setAccs();
    }
    public void setAccs(){
        List<Account>accList=[select id,name from Account limit 5];
        if(accList.size()>0){
            AccountList = accList;
            
        }
    }
    public void quoteSelected(){
        System.debug('checkedQuotes= '+checkedQuotes);
        //call your other VF page here
    }
    public PageReference addQuotesToChecked(){
        string quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        string chkValue = Apexpages.currentPage().getParameters().get('chkValue');
        System.debug('quoteId '+quoteId);
        System.debug('chkValue '+chkValue);
        if(checkedQuotes ==null){
            checkedQuotes= new List<id>();
            checkedQuotes.add(quoteId);
            
        }
        else{
            if(chkValue=='true'){
                checkedQuotes.add(quoteId);
            }
            else{
               integer i= checkedQuotes.indexOf(quoteId);
                checkedQuotes.remove(i);
            }
             
        }
       System.debug('checkedQuotes '+checkedQuotes);
        integer i=checkedQuotes.size();
       
        return null;
    }
}
regards
 
Alexander AtkinsonAlexander Atkinson
This works great thank you. It's logging the selected Ids now.

I'd like to ask one more question. How do I pass "checkedQuotes" into another controller. I am going to query the Ids within that controller and display them on my page.
Sampath SuranjiSampath Suranji
Hi,

I suggest you to use the same controller in both VF pages, Then you can reffer the selected Id list directly.
Please check below example,
modify the above controller as below
public PageReference quoteSelected(){
        System.debug('checkedQuotes= '+checkedQuotes);
        //call your other VF page here
        PageReference pr= new PageReference('/apex/devForum10_22_1');
        return pr;
    }

// add below method to return required list for the second VF page
public PageReference getSelectedQuotes(){
        try{
            LIst<Account> accList=[select id,name from Account where id in:checkedQuotes];
            if(accList!=null && accList.size()>0){
                selectedAccountList= accList;
            }
        }
        
        catch(Exception ex){}
        return null;
        
    }
your second VF page would be like this,
<apex:page controller="devForum10_22" action="{!getSelectedQuotes}">
    <apex:form>
        <h1>
            your second page
        </h1>
        
        <table>            
            <tr class="headerRow">
                
                <th>Id</th>
                <th>Name</th>
            </tr>
            <apex:repeat value="{!selectedAccountList}" var="r">
                <tr>
                    
                    <td><apex:outputField value="{!r.id}"/></td>
                    <td><apex:outputField value="{!r.name}"/></td>
                    
                </tr>
            </apex:repeat>
        </table>
    </apex:form>  
</apex:page>
regards
sampathjt@gmail.com

 
Ajay K DubediAjay K Dubedi
Hi Alexander,

Below code can fulfill your requirements, Hope this will work for you.

Vf Page :

<apex:page standardController="Opportunity" extensions="CreateQuoteClass">
    <apex:sectionHeader title="Opportunity Items"/>
    <apex:form >
        <apex:pageBlock title="Products Items">
            <apex:pageBlockTable var="qi" value="{!quotewrapperlist}" id="quoteitem">
                <apex:column headerValue="Action">
                    <apex:inputCheckbox value="{!qi.isChecked}"/>
                </apex:column>
                <apex:column headerValue="Product" value="{!qi.oliresult.name}"/>
                <apex:column headerValue="Quantity" value="{!qi.oliresult.Quantity}"/>
                <apex:column headerValue="Sales Price" value="{!qi.oliresult.Unitprice}"/>
                <apex:column headerValue="List Price" value="{!qi.oliresult.TotalPrice}"/>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Create Quote" action="{!saveQuote}" immediate="false"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>


Class :

public class CreateQuoteClass {
    public String opportunitystringId {get;set;}
    public List<Opportunity> opportunityList {get;set;}
    public List<quotewrapper> quotewrapperlist {get;set;}
    public List<OpportunityLineItem> oliList{get;set;}
    
    public class quotewrapper
    {
        public Boolean isChecked {get;set;}
        public OpportunityLineItem oliresult {get;set;}
        
        public quotewrapper(Boolean isChecked, OpportunityLineItem oliresult)
        {
            This.isChecked = isChecked;
            This.oliresult = oliresult;
        } 
    }   
    
    public CreateQuoteClass(ApexPages.StandardController controller)
    {
        try{
            quotewrapperlist = new List<quotewrapper>();
            opportunitystringId  = ApexPages.CurrentPage().getparameters().get('Id');
            
            if(opportunitystringId!=null)
            {
                opportunityList = [SELECT Id,Name,CloseDate,AccountId,Pricebook2Id from Opportunity WHERE Id =:opportunitystringId];
                oliList = [Select Id,Name,Quantity,OpportunityId,UnitPrice,Product2Id,PricebookentryId,TotalPrice from OpportunityLineItem WHERE OpportunityId =:opportunitystringId];
            }
            
            if(oliList.size()>0)
            {
                for(OpportunityLineItem olObj:oliList)
                {
                    quotewrapper qobj =  new quotewrapper(false, olObj);
                    quotewrapperlist.add(qobj);
                }
            }
        }
        catch(Exception e)
        {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        
    }
    
    public void saveQuote()
    {
        try{
            List<OpportunityLineItem> olilistNew = new List<OpportunityLineItem>();  
            List<QuoteLineItem>  quoteItemList = new List<QuoteLineItem>();
            
            for(quotewrapper qwr : quotewrapperlist)
            {
                if(qwr.isChecked==true)
                {
                    olilistNew.add(qwr.oliresult);
                }
            }
            
            List<Quote> quoteListNew = new List<Quote>();
            if(opportunityList.size()>0)
            {
                for(Opportunity opportunityObj : opportunityList)
                {
                    Quote quoteObj = new Quote();
                    quoteObj.Name=opportunityObj.Name;
                    quoteObj.OpportunityId = opportunityObj.Id;
                    quoteObj.Pricebook2Id =opportunityObj.Pricebook2Id;
                    quoteListNew.add(quoteObj); 
                }
            }
            if(quoteListNew.size()>0)
            {
                Database.SaveResult[] quoteItemSaveList = Database.insert(quoteListNew);
            }
            if(olilistNew.size()>0)
            {
                for(Quote qutObj: quoteListNew)
                { 
                    for(OpportunityLineItem oliObj : olilistNew)
                    {  
                        QuoteLineItem qlobj = new QuoteLineItem();
                        qlobj.Quantity=oliObj.Quantity;
                        qlobj.PricebookEntryId=oliObj.PricebookEntryId;
                        qlobj.QuoteId=qutObj.Id;
                        qlobj.Product2Id=oliObj.Product2Id;
                        qlobj.UnitPrice=oliObj.UnitPrice;
                        quoteItemList.add(qlobj);   
                    }
                }
                
                if(quoteItemList.size()>0)
                {
                    Database.SaveResult[] quoteItemSaveList = Database.insert(quoteItemList);
                }
            }
        } 
        
        catch(Exception e)
        {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }  
    }
}


Please mark this as best answer if this solves your problem.

Thank you,
Ajay Dubedi