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
Chelsea LukowskiChelsea Lukowski 

Help displaying variable from controller to visualforce page <apex:repeat>.

I need some advise. I am creating a visualforce page where a user can search for a product and see a price, then they can select a percentage to take off that price and the page would recalculate the price times that percentage. I can get the searh to work and a price to show up but it displays the same price for all products.

This is the Class i currently have:
public class ProductInventoryMobileController {
    
    public string searchstring {get;set;}
    public list<Inventory__c> invList {get;set;}
    
    public double discountsearchstring {get;set;}
    public list<Product2> prodList {get;set;}
    public Double dealerPrice {get; set;}
    
    public void search(){
        if(searchstring != Null){
            system.debug('Search String -' + searchstring);
            string searchquery = 'select Lookup_Key__c,Product__r.ProductCode,Product__r.Name,Weight_Per_U_M__c,Available__c,Warehouse__c,Warehouse_City__c,Product__r.Standard_Price__c,id from Inventory__c where Lookup_Key__c LIKE \'%'+searchstring+'%\' ORDER BY Product__r.ProductCode ASC';
            invList = Database.query(searchquery); 
        }else{
            invList = Database.query('select Lookup_Key__c,Product__r.ProductCode,Product__r.Name,Weight_Per_U_M__c,Available__c,Warehouse__c,Warehouse_City__c,Product__r.Standard_Price__c,id from Inventory__c ORDER BY Product__r.ProductCode ASC');
        }
    }
    
    public List<Product2> searchProduct(){        
        string searchquery = 'select Name, ProductCode,Search_String__c,Inventory_Count__c, Standard_Price__c from Product2 where Standard_Price__c < 99999.99 AND ProductCode LIKE \'%'+searchstring+'%\' ORDER BY ProductCode ASC';
        system.debug('Discount String - ' + discountsearchstring);
        dealerPrice = 0;
        for(Product2 p: database.query(searchquery) ){
            if(discountsearchstring != 0.0){
        		dealerPrice = discountsearchstring * p.Standard_Price__c;
                system.debug('dealer price - '+ dealerPrice); 
            }else {
                dealerPrice = p.Standard_Price__c;
                system.debug('dealer price - '+ dealerPrice);
            }
        }
        prodList = database.query(searchquery);
        return ProdList;
    }
    
    public pageReference returnProducts(){
        searchProduct();
        return null;
    }
    
    public void clearProducts(){
        searchstring = '';
        prodList.clear();
    }
    
    public void clear(){
        searchstring = '';
        invList.clear();
    }
}
This is the visualforce page:
<apex:page Controller="ProductInventoryMobileController" standardStylesheets="false" readOnly="true" docType="html-5.0">
    
    <apex:form >
        <apex:tabPanel switchType="client" selectedTab="sch" id="dataTabPanel">
            <apex:tab label="Inventory" name="Inventory Search" id="sch">
                <br/>
                <apex:inputText value="{!searchstring}" label="Input"/> 
                <apex:commandButton value="Search" action="{!search}"/> 
                <apex:commandButton value="Clear" action="{!clear}"/> 
                <p>
                    Search Results
                </p> 
                <c:ProductInventoryMobile />
                <table>
                    <tr>
                        <th>Product Code</th>
                        <th>Available</th>
                        <th>List Price</th>
                        <th>Warehouse</th>
                        <th>Weight Per U/M</th>
                        
                    </tr>
                    <apex:repeat value="{!invList}" var="i">
                        <tr>
                            <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">{!i.Product__r.ProductCode}</td>
                            <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">{!i.Available__c}</td>
                            <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">${!i.Product__r.Standard_Price__c}</td>
                            <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">{!i.Warehouse__c}</td>
                            <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">{!i.Weight_Per_U_M__c}</td>
                        </tr>                    
                    </apex:repeat>
                </table>
            </apex:tab>
            <apex:tab label="Pricing" name="Pricing Calculator" id="sch2">
                <br/>
                <apex:inputText value="{!searchstring}" label="Input"/> 
                <apex:commandButton value="Search" action="{!returnProducts}"/> 
                <apex:commandButton value="Clear" action="{!clearProducts}"/> 
                <br/>
                <apex:outputLabel for="discount" value="On-Factor:"></apex:outputLabel>&nbsp;&nbsp;&nbsp;
                    <apex:selectList id="discount" value="{!discountsearchstring}" size="1" label="On-Factor:">
                        	<apex:SelectOption itemValue="0.0" itemLabel=""/>
                            <apex:SelectOption itemValue=".45" itemLabel="45"/>
                            <apex:SelectOption itemValue=".44" itemLabel="44"/>
                            <apex:SelectOption itemValue="54" itemLabel="54"/>
                            <apex:SelectOption itemValue="60" itemLabel="60"/>
                     <apex:actionSupport event="onchange" action="{!returnProducts}"/>
                    </apex:selectList>
                
                <p>
                    Search Results
                </p>
                    <table>
                        <tr>
                            <th>Product Code</th>
                            <th>Inventory</th>
                            <th>Price</th> 
                        </tr>
                        <apex:repeat value="{!prodList}" var="p" id="list">
                            <tr>
                                <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">{!p.ProductCode}</td>
                                <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">{!p.Inventory_Count__c}</td>
                                <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;"> ${!dealerPrice}</td>
                            </tr>                    
                        </apex:repeat>
                    </table>
            </apex:tab>
        </apex:tabPanel>
    </apex:form> 
</apex:page>
Here is what is displaying, and it should be showing different prices for each product. 
User-added image 
With the discount:
User-added image
 
Best Answer chosen by Chelsea Lukowski
Alain CabonAlain Cabon
Hi,

dealerPrice is a unique value in the controller.

public Double dealerPrice {get; set;}    // ${!dealerPrice}

The following formula should be in the VFP and not in the Apex code:

dealerPrice = discountsearchstring * p.Standard_Price__c;

 <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">
       ${! IF(  discountsearchstring != 0  , discountsearchstring   * p.Standard_Price_c , p.Standard_Price_c ) }
 </td>

Caution: don't use LIKE \'%'+searchstring+'%\'   : To prevent a SOQL injection attack, avoid using dynamic SOQL queries. Instead, use static queries and binding variables. The vulnerable example above can be re-written using static SOQL as follows:

https://developer.salesforce.com/docs/atlas.en-us.securityImplGuide.meta/securityImplGuide/review_and_certification.htm

Best regards.
Alain

All Answers

Alain CabonAlain Cabon
Hi,

dealerPrice is a unique value in the controller.

public Double dealerPrice {get; set;}    // ${!dealerPrice}

The following formula should be in the VFP and not in the Apex code:

dealerPrice = discountsearchstring * p.Standard_Price__c;

 <td style="page-break-inside: avoid;border:1px solid black;padding: 7px;word-wrap: break-word;text-align:center;">
       ${! IF(  discountsearchstring != 0  , discountsearchstring   * p.Standard_Price_c , p.Standard_Price_c ) }
 </td>

Caution: don't use LIKE \'%'+searchstring+'%\'   : To prevent a SOQL injection attack, avoid using dynamic SOQL queries. Instead, use static queries and binding variables. The vulnerable example above can be re-written using static SOQL as follows:

https://developer.salesforce.com/docs/atlas.en-us.securityImplGuide.meta/securityImplGuide/review_and_certification.htm

Best regards.
Alain
This was selected as the best answer
Chelsea LukowskiChelsea Lukowski
Thank you so much that worked! I will work on change the dynamic query to static query and binding varible. Appreiciate the help!