Activity Forums Salesforce® Discussions How to make multiselect checkboxes in searched results of any Salesforce sObject?

  • How to make multiselect checkboxes in searched results of any Salesforce sObject?

    Posted by PRANAV on September 6, 2016 at 3:46 pm

    Hi All,

    How to make multiselect checkboxes in searched results of any Salesforce sobject?

    As in my scenario, there is a search box which searches the user's entered value and shows the result via search button click. So I want to select multiple results (through checkbox) to use it further. How it can be done help me out.

    Thanks

    Mohit replied 7 years, 7 months ago 2 Members · 1 Reply
  • 1 Reply
  • Mohit

    Member
    September 16, 2016 at 7:11 am

    Hi Pranav,

    You have to create a Visualforce page to show data and you have to make controller for it to perform operation.

    Here is Visualforce page:-
    <apex:page standardController="Opportunity" extensions="OpportunityProductsearch">
    <apex:form id="theForm">
    <apex:pageBlock title="Search the Product" >
    <apex:inputText value="{!searchtext}"/>
    <apex:commandButton value="search" action="{!search}" />
    <apex:pageMessages />
    <apex:pageBlockSection Title="List of Product" id="table" collapsible="false" rendered = "{!toshow}">
    <apex:pageBlockTable value="{!listofproductselected}" var="p" >
    <apex:column title="Select" >
    <apex:inputCheckbox value="{!p.checked}" ></apex:inputcheckbox>
    </apex:column>
    <apex:column headervalue="Name" value = "{!p.pro.Name}" />
    <apex:column headervalue="Product Code" value="{!p.pro.ProductCode}"/>
    <apex:column headervalue="Description" value="{!p.pro.Description}" />
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    <apex:commandButton value ="Submit" action ="{!submit}" rendered="{!toshow}" />
    <apex:pageBlockSection Title="List of Product in Update" id="table2" collapsible="false" rendered = "{!!toshow}">
    <apex:pageBlockTable value="{!showprod}" var="s" >
    <apex:column headervalue="Name" >
    <apex:inputField value = "{!s.Name}" />
    </apex:column>
    <apex:column headervalue="Product Code">
    <apex:inputField value="{!s.ProductCode}"/>
    </apex:column>
    <apex:column headervalue="Description">
    <apex:InputField value="{!s.Description}" />
    </apex:column>
    </apex:pageBlockTable>
    </apex:pageBlockSection>
    <apex:commandButton value ="save" action ="{!autoupdate}" rendered="{!!toshow}" />
    <apex:commandButton value ="cancel" action ="{!search}" rendered="{!!toshow}" />
    </apex:pageBlock>
    </apex:form>
    </apex:page>

    and Now the Controller:-
    public with sharing class OpportunityProductsearch {
    public String searchText{get;set;}
    public boolean toshow{get;set;}
    public List<Product2> prod{get;set;}
    public list<ProductWrapper> listofproductselected{get;set;}
    public list<Product2> selectedproduct;
    public list<Product2> showprod{get;set;}

    public class ProductWrapper{
    public Product2 pro{get;set;}
    public Boolean checked{get; set;}
    public ProductWrapper(Product2 p,boolean b){
    pro = p;
    checked = b;
    }
    }
    public OpportunityProductsearch(ApexPages.StandardController con){
    toshow = true;
    prod = new List<Product2>();
    prod.clear();
    }
    public void search(){
    prod.clear();
    toshow = true;
    listofproductselected = new list<ProductWrapper>();

    if(!String.isBlank(searchText)){
    String s = '%'+searchText+'%';
    prod =[select Name,ProductCode,Description,family from Product2 where name like :s ];

    if(prod!=null){
    for(Product2 p : prod){
    ProductWrapper pw = new ProductWrapper(p,false);
    listofproductselected.add(pw);
    }
    }
    }
    }
    public void submit(){
    toshow = false;
    selectedproduct = new list<Product2>();
    for(ProductWrapper p : listofproductselected ){
    if(p.checked == true){
    selectedproduct.add(p.pro);
    }
    }
    System.debug('the select product to be printed : '+selectedproduct);
    showprod = selectedproduct;
    }
    public void autoupdate(){
    update showprod;
    search();
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos