Is it possible to multiply this values, something like this: Qty__c * NRPrice__c in the query below

PODetails2 = [SELECT  Qty__c, NRPrice__c  FROM o_Bundle__c  ];

or I have to do it in the VF page in order to add the Total value, if so HOW?

<apex:dataTable value="{!PODetails2}" var="b" >
    <apex:column  headerValue="Qty" value="{!b.Qty__c}" />
    <apex:column  headerValue="Rate" value="{!b.Bundle_Item__r.NRPrice__c}" />
    <apex:column  headerValue="Total" value="MULTIPLY VALUES" />    

Thank you in advance!!!

Here is the solution by AmatorVitae:

  • 1
    Hi - I see you've changed the title to include "Solved" it would be good if you can accept the answer? This will help others with the same issue :) – Doug B Feb 14 '14 at 16:53

Try doing the multiplication in the VF Page. Anything in between {! and } is a formula.

<apex:dataTable value="{!PODetails2}" var="b" >
    <apex:column  headerValue="Qty" value="{!b.Qty__c}" />
    <apex:column  headerValue="Rate" value="{!b.Bundle_Item__r.NRPrice__c}" />
    <apex:column  headerValue="Total" value="{!b.Qty__c*b.Bundle_Item__r.NRPrice__c}" />    

EDIT - This above approach does not work. See below for the solution

Here's a recap of the conversation in the comments:

You are setting PODetails2 via the SOQL query:

PODetails2 = [SELECT Bundle_Item__c, 
              FROM o_Bundle__c 
              WHERE Bundle__c IN:contractIDs ];

So, in your VF page, create the pageBlockTable as follows. The trick is to do the multiplication between apex:column tags. Apex will display almost anything between apex:column tags, but if in stead you put the value in the value tag, then Apex does some special coding in choosing how to display the value (as a number? or as a hyperlink in case of a Name field or Lookup Field?, etc.) So the solution is to hot-wire apex:column by using the first approach.

Inside {! and }, Apex process the information as a formula returning text. So what you can do is wrap the formula inside an apex:output tag, and then use the tag to do the formatting.

    <apex:pageBlockTable value="{!PODetails2}" var="x">
        <apex:column headerValue="Quantity" value="{!x.Qty__c}"/>
        <apex:column headerValue="Rate" value="{!x.Bundle_Item__r.NRPrice__c}"/>
        <apex:column headerValue="Total">
            <apex:outputText value="{0, number,$###,###,##0.00}">
                <apex:param value="{!x.Qty__c*x.Bundle_Item__r.NRPrice__c}" />

PS I used a pageBlockTable instead of a dataTable. The only difference is the pageBlockTable has some nice styling attached to it.

| improve this answer | |
  • I got this error message: Error: Incorrect parameter type for operator '*'. Expected Number, received Object. I don't know why because Qty and NRPrice are mumber fields. Any Ideas? – Carlos Feb 13 '14 at 21:38
  • can we cast both as numbers? i'm checking the function list. i tested the code, and it worked on my end. – Scott Pelak Feb 13 '14 at 22:03
  • Thank you for your time, i really appreciate it... I am so new in apex. How would you cast these values in the VF page? – Carlos Feb 13 '14 at 22:08
  • try checking your controller to make sure the parameters are what you think they are. Also, try making a new VF page with a StandardController on the sOjbect you're using, and test out a simple example. Example: <apex:page standardController="Paycheck__c"> <apex:outputText value="{!Paycheck__c.Hours__c*Paycheck__c.TotalBenefit__c+Paycheck__c.Pay_Run__r.CKcount__c}"/> – Scott Pelak Feb 13 '14 at 22:11
  • For casting, you can only cast a string to decimal via VALUE and anything to a string via TEXT. Concerning the error code "Expected Number, received Object", that means that one of the two parameters (either b.Qty__c or b.Bundle_Item__r.NRPrice__c) is an Object not a "field" of an object -- which is what we're looking for in this example – Scott Pelak Feb 13 '14 at 22:15

You can't do it in SOQL, but you can create a formula field on the object that can be queried. In all likelihood, if you need the calculation here, you'll need it somewhere else such as a report. By making a formula field, you can ensure everyone is using the same calculation.

| improve this answer | |
  • If I can't do it by code, I will use the formula field. Thank you for your advice! – Carlos Feb 14 '14 at 0:01

Best Idea in this case should be creating wrapper class to manage all calculations and make displaying easier

For example

class wrapper{

   Integer reult;
   o_Bundle__c object;

   public void wrapper(o_Bundle__c b){
      reult = /** some operations **/
      object = b;

thanks to that you will have access to object and calculated values

| improve this answer | |

Use a Wrapper class then. Have the table iterate over a list of the wrapper instead.

public class MyWrapper
  public o_Bundle__c bundle{get; set;}

  public MyWrapper(o_Bundle__c bundle)
    this.bundle = bundle;

  public Integer getMult()
    return bundle.Qty__c * bundle.Bundle_Item__r.NRPrice__c;
| improve this answer | |
  • That will be the ideal fix, but we are trying to remove fields, so I am not allow to add more fields because the system is running too slow. Thank you for your answer! – Carlos Feb 13 '14 at 19:03
  • @Carlos This doesn't add any fields... – dphil Sep 2 '14 at 23:00
  • hi dphil, now that i am more familiar with wrapper classes I went back to this question and i realized that your solution is correct too. Thanks! – Carlos Sep 2 '14 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.