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
dblazedblaze 

Multipy a value inside apex repeat or in controller query

Hi Everyone,

After banging my head against the desk repetedely for the past few days I decided to see if any of you wonderful people in the community could help me out with an issue I am having.  A little backstory....my project has two pages, the first to select a recipe and then the second to pass recipe yield multiplier with the recipe Id to the second visualforce page that pulls the ingredients.  On this second visualforce page, I want the value they enter (that gets passed to the next page) double, triple, etc the ingredients actual amount.  For example: to make an egg sandwich, the base yield requires one egg.  To make three egg sandwiches, (Base 1) * (Multiplier 3 )=3

 

Listed below is the output based on the parameters passed in the url:

 

3.0 * 123 Cup Onions 1/4" Diced
3.0 * 123 Cup Green Bell Pepper 1/4" Diced
9.0 * 123 Cup Scramble Egg Mix
3.0 * 123 Cup Cheddar Cheese, Shredded

 

123 is what I am passing from the first page, and the * is added into the apex repeat

 

<apex:pageBlock title="Multiplied Ingredients">
<apex:repeat value="{!Ingredients}" var="Ingredient">
<tr>
<td align="right"> 
<apex:outputText value="!Ingredient.Amount__c} * {!YieldMultiplier}"/></td>
<td align="right"><apex:outputField value="{!Ingredient.Unit__c}"/></td>
<td align="right">
<apex:outputField value="!Ingredient.Ingredient__c}"/></td><br/>
</tr>
</apex:repeat>
</apex:pageBlock> 

 and here is the query

public string YieldMultiplier {get;set;}

//Get Ingredients
public List<Ingredients__c> getIngredients()

     {
List<Ingredients__c> outIngredients = new List<Ingredients__c>();
            outIngredients = [select Unit__c, Amount__c, Ingredient__c, Recipe__c, Multiplied_Amount__c 
FROM Ingredients__c 
WHERE Recipe__c = : ApexPages.currentPage().getParameters().get('id')];
            
for (Ingredients__c ar : outIngredients)  {
     
// ar.Amount__c = decimal.valueOf(ar.Amount__c) *= YieldMultiplier;
// not sure if this is needed       
     }
     return outIngredients;

}

 Any help would be greatly appreciated!!

Best Answer chosen by Admin (Salesforce Developers) 
BritishBoyinDCBritishBoyinDC

Try this:

 

<apex:outputText value="{!Ingredient.Amount__c * YieldMultiplier}"/></td>

 

For reference, this simple example worked for me as a way to test it out:

 

public with sharing class mtest {

    public Integer yield {get;set;}
    
    public List<Account> getAccounts() {
        return [Select Id, Name, NumberOfEmployees from Account WHERE NumberOfEmployees != null];
    }

}

 

<apex:page controller="mtest" >
<apex:form >
<apex:inputText value="{!yield}"/>

<apex:repeat value="{!accounts}" var="a">
<apex:outputField value="{!a.name}"/> :
<apex:outputField value="{!a.NumberOfEmployees}"/>:

<apex:outputText value="{!a.NumberOfEmployees * yield}"/>
<br/>
</apex:repeat>

</apex:form>

</apex:page>

 Load the page, enter 4 and hit return - and the numberofemployees appears mutiplied by 4