• kunal kaushik 13
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Example : String str = ' SELECT Name,Id FROM Account LIMIT 10';
Now i want to retrieve "Account" from this query for further use.
// controller
public  with sharing class ManageMyProductPurchaseController {
    public String myPurchaseHistory { get; set;}
    public Boolean isAllProductsListVisible { get; set;}
    public Integer numOfRecords { get; set;}
    public Integer resultSize { get; set;}
    public String searchProductName { get; set;}
    public String allProductsQuery;
    public ApexPages.StandardSetController con {
        get {
            if(con == null) {
                con = new ApexPages.StandardSetController(Database.query(allProductsQuery));
                con.setpagesize(numOfRecords);
            }
            
            return Con;
        }
        set;
    }
  
        
    public ManageMyProductPurchaseController(){
        myPurchaseHistory = 'Purchase Order History';
        numOfRecords = 10;
    }
   
    public List<Product2> getAllProducts(){
        searchProductName = Apexpages.currentPage().getParameters().get('searchByName');
        if(String.isBlank(searchProductName)){
            System.debug('search field empty');
        	allProductsQuery = 'Select Name,ProductCode,cost__c,availableQuantity__c,isSelected__c FROM Product2';    
        }
        else{
            System.debug('search field not empty');
        	allProductsQuery = 'Select Name,ProductCode,cost__c,availableQuantity__c,isSelected__c FROM Product2 WHERE Name LIKE \'%'+searchProductName+'%\' Order By Name';    
        }
        resultSize = con.getResultSize();
        return con.getRecords();        
    }
    
    public void activateShowCatalog(){
        isAllProductsListVisible = true;
    }
    
    public void addProductsToCart(){
        isAllProductsListVisible = false;        
    }

    public PageReference sortProductsByName(){
        searchProductName = Apexpages.currentPage().getParameters().get('searchByName');
        System.debug('++'+searchProductName+'++');
        if(String.isEmpty(searchProductName) || searchProductName.equalsIgnoreCase('')){
            allProductsQuery = 'Select Name,ProductCode,cost__c,availableQuantity__c,isSelected__c FROM Product2';
        	System.debug('**');
        }
        else{
            allProductsQuery = 'Select Name,ProductCode,cost__c,availableQuantity__c,isSelected__c FROM Product2 WHERE Name LIKE \'%'+searchProductName+'%\' Order By Name';
            System.debug('--');
            System.debug('All products : '+allProductsQuery);
        }
        
        return null;
    }
    
    public void onSelectCheckBox(){
        System.debug('Checked');
        // I want to use the product which is checked in the checkbox here
    }
}

visualforce page :
 
<apex:page controller="ManageMyProductPurchaseController">
   <head>
   		<apex:slds />    
   </head>
    <script>
    	function doSearch() {
        searchServer(
          document.getElementById("searchByName").value);
        }
    </script> 
    <apex:form >
    <apex:pageBlock title="{! myPurchaseHistory}" id="productPurchaseHistory">
    	<apex:pageBlockSection ></apex:pageBlockSection>
    	<apex:pageBlockButtons >
        	<apex:commandButton action="{!activateShowCatalog}" value="Add New Purchase"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
    
    
    <apex:pageBlock title="Products" rendered="{! if(isAllProductsListVisible == true,true,false)}">
        	<apex:actionFunction name="searchServer" action="{! sortProductsByName}" rerender="allProductList">
				<apex:param name="searchByName" value=""/>
			</apex:actionFunction>
        	<apex:pageBlockSection >
        		<input type="text" id="searchByName" onKeyUp="doSearch();"/>
        	</apex:pageBlockSection>
        	<apex:pageBlockTable value="{!allProducts}" var="product" id="allProductList">
                
                <apex:column value="{!product.productCode}"/>
                <apex:column value="{!product.Name}"/>
                <apex:column value="{!product.cost__c}"/>
            	<apex:column value="{!product.availableQuantity__c}"/>
                <apex:column headerValue="Selected Products" >   
				<apex:inputCheckbox value="{!product.isSelected__c}" />  // i want to use the product corresponding to this checkbox in the function onSelectCheckBox()
                </apex:column> 
        	</apex:pageBlockTable>
        <apex:outputText >Page {!(con.pageNumber)} of {! CEILING(resultSize / con.pageSize) }</apex:outputText>
         <apex:commandButton rendered="{!con.hasPrevious}" value="first" action="{!con.first}"/>
         <apex:commandButton rendered="{!con.hasPrevious}" value="Previous" action="{!con.previous}"/>
         <apex:commandButton rendered="{!con.hasNext}" value="next" action="{!con.next}"/>
         <apex:commandButton rendered="{!con.hasNext}" value="last" action="{!con.last}"/>
        
        	<apex:pageBlockSection >
        		<apex:commandButton action="{!addProductsToCart}" value="add Products To Cart"/>
        	</apex:pageBlockSection>
    </apex:pageBlock>     
        
    <apex:pageBlock title="Cart Items" rendered="{!if(isAllProductsListVisible,false,true)}">
    	    
    </apex:pageBlock>    
   </apex:form>
    
</apex:page>

Please help me out with this