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
Sugandhi VermaSugandhi Verma 

Help!!!!!! adding field values using apex:variable

Hiii,

I am adding the field value  with the apex:variable but getting the error--"Incorrect parameter type for operator '+'. Expected Text, received Number" Below is my code,please helppp:
<apex:variable value="{!0.00}" var="total1"/>
        <apex:pageBlock >
                     <apex:pageBlockTable title="Your Item" value="{!selectedWrapperList}" var="wrapRec" rendered="{!selectedList}" >
                    
 <apex:column value="{!wrapRec.Name}" />
              
               <apex:column ><apex:outputField value="{!wrapRec.T_Price__c}" />
               <apex:variable var="total1" value="{! total1 + wrapRec.T_Price__c}" />

        <!-- Footer of the first Cell -->>
        <apex:facet name="footer">
            Total:{!total1}  <span class="t1"></span>
        </apex:facet>
               </apex:column>
 </apex:pageBlockTable> 
             
              </apex:pageBlock>

   <script>
   document.getElementsByClassName('t1')[0].innerHTML = '{!total1}';
   </script>

 
Mudasir WaniMudasir Wani
Hi Sugandhi,

Use the below code 
<apex:variable value="{!0.00}" var="total1"/>
        <apex:pageBlock >
                     <apex:pageBlockTable title="Your Item" value="{!selectedWrapperList}" var="wrapRec" rendered="{!selectedList}" >
                    
 <apex:column value="{!wrapRec.Name}" />
              
               <apex:column ><apex:outputField value="{!wrapRec.T_Price__c}" />
               <apex:variable var="total1" value="{! total1 + String.ValueOf(wrapRec.T_Price__c)}" />

        <!-- Footer of the first Cell -->>
        <apex:facet name="footer">
            Total:{!total1}  <span class="t1"></span>
        </apex:facet>
               </apex:column>
 </apex:pageBlockTable> 
             
              </apex:pageBlock>

   <script>
   document.getElementsByClassName('t1')[0].innerHTML = '{!total1}';
   </script>

Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
Sugandhi VermaSugandhi Verma
Hi Mudasir,

It's Java datatype Conversion method.It is not working in the apex:variable statement.
Error-Unknown function "String.ValueOf"
Mudasir WaniMudasir Wani
Hey Sugandhi,

Pasete controller sample code here.
 
Sugandhi VermaSugandhi Verma
public List<Tour_Places__c> selectedWrapperList{ get; set;}
    public void ProceedWithSelected() {
      selectedWrapperList = new List<Tour_Places__c>();
         normalList = false;
         selectedList = true;
         for(Tour_Places__c selectedWrapObj: tourInformation){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.Select__c);
            if(selectedWrapObj.Select__c == true)
            selectedWrapperList.add(selectedWrapObj);
        }
         system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
  }


my controller contained the above code...
JayantJayant
Use below in formulas/merge  fields - 

1. & - Ampersand character to concatenate 2 strings.
2. TEXT() - To convert a number to string.
3. VALUE() - To convert a string to number.

You may use any function available in Formula Fields in VF Merge Fields (except a few).
Mudasir WaniMudasir Wani
Hey Sugandhi,

Note:<apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined.

And you are trying to use it  inside of an iteration 
Have a look on the documentation 
https://www.salesforce.com/docs/developer/pages/Content/pages_compref_variable.htm




Donot forget to select best answer to make our efforts visible in the developer forum.
Please mark this as solution by selecting it as best answer if this solves your problem, So that if anyone has this issue this post can help
 
Sugandhi VermaSugandhi Verma
Hi Jayant,

It's showing the wrong results as i am adding a field of number type and a variable in value attribute.
 
Sugandhi VermaSugandhi Verma
Hi Mudasir,

Actually this code is working fine...m using this concept..
<apex:page controller="MyTestsumtotal">
<apex:form >

<!-- Need this variables for totals generation -->
<apex:variable value="{!0.00}" var="total1"/>
<apex:variable value="{!0}" var="total2"/>

<apex:pageBlock title="Totals">
<apex:pageBlockTable value="{!accounts}" var="item" id="list1">

    <apex:column value="{!item.name}" width="20%"/>

    <apex:column headerValue="Number1">
        <apex:outputField value="{!item.number1__c}" />

        <!-- Generating a first total -->
        <apex:variable var="total1" value="{!total1 + item.number1__c}" />

        <!-- Footer of the first Cell -->>
        <apex:facet name="footer">
            Total: <span class="t1"></span>
        </apex:facet>
    </apex:column>

    <apex:column headerValue="Number2">
        <apex:outputField value="{!item.number2__c}" />
        <apex:variable var="total2" value="{!total2 + item.number2__c}" />
        <apex:facet name="footer">
            Total: <span class="t2"></span>
        </apex:facet>
    </apex:column>

</apex:pageBlockTable>
</apex:pageBlock>

<script>
    // Here we will set the generated subtotals to the footer cells
    document.getElementsByClassName('t1')[0].innerHTML = '{!total1}';
    document.getElementsByClassName('t2')[0].innerHTML = '{!ROUND(total2,0)}';
</script>

</apex:form>
</apex:page>

my class is:
public class MyTestsumtotal {
  public List<Account> accounts { get; set; }

    public MyTestsumtotal(){
        accounts = [select id, name, number1__c, number2__c 
                    from account 
                    where number1__c != null and 
                    number2__c != null];
    }
}

 
JayantJayant

The variable 'total1' is of Object type, logically you need to convert it to a decimal before adding.
So {!(VALUE(total1) + wrapRec.T_Price__c)} or {!(VALUE(TEXT(total1)) + wrapRec.T_Price__c)} may work.

Disclaimer : I have not really tried this on a Page but this seems logical, if addition can happen in the value attribute.
Sugandhi VermaSugandhi Verma
Hi Jayant,

I have tried that also but it's not working.
Mudasir WaniMudasir Wani
Have you checked the data type of

number1__c and  T_Price__c

Is there any difference.

 
Sugandhi VermaSugandhi Verma
No, there are no difference. Both are number type.