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
krishna23krishna23 

Add selected records to another section on same page in VF

I am displaying account records in one section with a checkbox.  Now I want to add selected records to another section on the same page. 

Thanks...
Best Answer chosen by krishna23
Gulafsha MohammedGulafsha Mohammed
Hi Krishna,
Selected records can be stored in a list and fetched from controller by the other page block.

You may keep an add button to call the apex method and store the list values of selected checkboxes. Wrapper class can be used for easy retrieval of selected records.

Whenever add button is clicked render that block.

Hope this helps. Please mark this as best answer if so.
Regards,
Gulafsha

All Answers

Gulafsha MohammedGulafsha Mohammed
Hi Krishna,
Selected records can be stored in a list and fetched from controller by the other page block.

You may keep an add button to call the apex method and store the list values of selected checkboxes. Wrapper class can be used for easy retrieval of selected records.

Whenever add button is clicked render that block.

Hope this helps. Please mark this as best answer if so.
Regards,
Gulafsha
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi krishna,

Below Sample 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