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
Rafael.Martins.SantosRafael.Martins.Santos 

How show in the visualpage the value of an variable?

Hi All,

I'm trying create a visualpage that show the total amount of all opportunities closed.
In apex I did, but I don't know how I could display the results in visualpage.

My Apex code and visual page are like this:

APEX Code:
public class SumTotal {
    
        public Double SumOpportunities(){
        List<Opportunity> op = new List<Opportunity>();
        
        op = [SELECT Id, Name, Amount, StageName FROM Opportunity WHERE Amount > 0 AND StageName ='Fechado e ganho'];
        Double sum = 0.00;
            for(Opportunity opty : op){
                
                sum = sum + opty.Amount;
                System.debug(' Name : '+opty.Name+', Sum: '+soma + ', Opty Amount: '+opty.Amount);
            }
        return sum;
        System.debug('Sum: '+sum);
    }
}


VisualPage:

<apex:page showHeader="false" sidebar="false" standardStylesheets="false"
applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" controller="SumTotal">
<html>
<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"></meta>
    <title>Teste</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <apex:dataTable value="{!Here I don't know what put}" var="sum">
            <apex:column>
            <apex:facet name="header">Product</apex:facet>
            <apex:outputText value="{!Here I must display the results of the variable}" />
            </apex:column>
        </apex:dataTable>
        </div>

Thanks
Rafael
    </div>
</body>
</html>

</apex:page>
Best Answer chosen by Rafael.Martins.Santos
DeveloperSudDeveloperSud
Hi ,

Please refer the below page.Let us know if this works.
 
<apex:page showHeader="false" sidebar="false" standardStylesheets="false"
applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" controller="SumTotal1">
<html>
<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"></meta>
    <title>Teste</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <apex:dataTable value="{!SumOpportunities}" var="s">
            <apex:column value="{!s.name}">
            <apex:facet name="header">Product</apex:facet>
            </apex:column>&nbsp;&nbsp;
            <apex:column value="{!s.amount}">
            <apex:facet name="header">Amount</apex:facet>
            </apex:column>&nbsp;&nbsp;
        </apex:dataTable>
         <apex:outputText value="------------------" style="padding:0px 0px 0px 230px"/><br/>
         <apex:outputText value="Total Amount" />
         <apex:outputText value="${!sum}" style="padding:0px 0px 0px 150px"/>
        </div>
    </div>
</body>
</html>

</apex:page>
 
public with sharing class SumTotal1 {

   public double Sum{get;set;}
   
   public SumTotal1(){
   
   sum=0;
   }
  
public List<Opportunity> getSumOpportunities(){
        List<Opportunity> op = [SELECT Id, Name, Amount, StageName FROM Opportunity WHERE Amount > 0 AND StageName ='closed Won'];
        
        //Double sum = 0.00;
            for(Opportunity opty : op){
                
                sum = sum + opty.Amount;
              //  System.debug(' Name : '+opty.Name+', Sum: '+soma + ', Opty Amount: '+opty.Amount);
            }
        return op;
      //  System.debug('Sum: '+sum);
    }
}

 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Use below upated code. 


Class:
public class SumTotal {

public List<Opportunity>  Opportunitieslist{get;set;}
public double sum{get;set;}
    
        public  SumTotal (){
        sum=0.00;
        
        Opportunitieslist= [SELECT Id, Name, Amount, StageName FROM Opportunity where amount >0 ];
        
            for(Opportunity opty : Opportunitieslist){
                
                System.debug(opty.Amount);
                sum += opty.Amount;
                System.debug(' Name : '+opty.Name+', Sum: '+sum + ', Opty Amount: '+opty.Amount);
            }
        System.debug('Sum: '+sum);
       
        
    }
}

Visualforce page: 
 
<apex:page controller="SumTotal" showHeader="false" sidebar="false" standardStylesheets="false"
applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" >
<html>
<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"></meta>
    <title>Teste</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <apex:dataTable value="{!Opportunitieslist}" var="opp"  styleClass="tableClass">>
            <apex:column>
            <apex:facet name="header">Name</apex:facet>
            
            <apex:outputText value="{!opp.name}"/>
        </apex:column>
        
        <apex:column>
            <apex:facet name="header">Stage Name</apex:facet>
             
            <apex:outputText value="{!opp.StageName}"/>
        </apex:column>

        <apex:column>
            <apex:facet name="header">Amount</apex:facet>
             
            <apex:outputText value="{!opp.Amount}"/>
        </apex:column>                    
        </apex:dataTable>
        <center>                         
             <h3>Total Amount: </h3> <apex:outputText value="{!sum}"/>            
            </center>
        </div>
        
            
    </div>
</body>
</html>

</apex:page>

Hope this will helps you. mark it solved if its work for you. 

Thanks
karthik

 
DeveloperSudDeveloperSud
Hi ,

Please refer the below page.Let us know if this works.
 
<apex:page showHeader="false" sidebar="false" standardStylesheets="false"
applyHtmlTag="false" applyBodyTag="false" docType="html-5.0" controller="SumTotal1">
<html>
<head>
    <meta charset="utf-8"></meta>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"></meta>
    <title>Teste</title>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
</head>
<body>
    <div class="container">
        <div class="jumbotron">
            <apex:dataTable value="{!SumOpportunities}" var="s">
            <apex:column value="{!s.name}">
            <apex:facet name="header">Product</apex:facet>
            </apex:column>&nbsp;&nbsp;
            <apex:column value="{!s.amount}">
            <apex:facet name="header">Amount</apex:facet>
            </apex:column>&nbsp;&nbsp;
        </apex:dataTable>
         <apex:outputText value="------------------" style="padding:0px 0px 0px 230px"/><br/>
         <apex:outputText value="Total Amount" />
         <apex:outputText value="${!sum}" style="padding:0px 0px 0px 150px"/>
        </div>
    </div>
</body>
</html>

</apex:page>
 
public with sharing class SumTotal1 {

   public double Sum{get;set;}
   
   public SumTotal1(){
   
   sum=0;
   }
  
public List<Opportunity> getSumOpportunities(){
        List<Opportunity> op = [SELECT Id, Name, Amount, StageName FROM Opportunity WHERE Amount > 0 AND StageName ='closed Won'];
        
        //Double sum = 0.00;
            for(Opportunity opty : op){
                
                sum = sum + opty.Amount;
              //  System.debug(' Name : '+opty.Name+', Sum: '+soma + ', Opty Amount: '+opty.Amount);
            }
        return op;
      //  System.debug('Sum: '+sum);
    }
}

 
This was selected as the best answer
Rafael.Martins.SantosRafael.Martins.Santos
Hi DeveloperSud,

Thanks for the help, it works.
But I don't understand one thing.

This line 13: "{!SumOpportunities}" var="s"
and this line 23 : value="${!sum}"

I see you not refer the "SumOpportunities" and the "sum" in the Apex code, and only add it to the visualpage.
How the visualpage understand that have to bring the results from apex (I know the relationship is connected by controller). 
I have a difficult to understand how it works.
Can you explain to me  please?

Thanks again
Rafael
DeveloperSudDeveloperSud
Hi Rafael,

I refered those variables in my visualforce page.In line 13 I have used "{!SumOpportunities}"  to refer the method SumOpportunities which is returning the list of opportunity records and for line 23 I have used "${!sum}" to show the total amount using the variable sum from the code.'$' just used as prefix to show the amount in USD.You can achieve this using apex properities .To know more about it please follow the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_properties.htm

Please mark this question as solved if this solved your problem.
Rafael.Martins.SantosRafael.Martins.Santos
Hi DeveloperSud,

So the "SumOpportunities" is the method that you called as "getSumOpportunities()", is that right?
Because this is hard to understand. To me, in the "{!SumOpportunities}" should be {!getSumOpportunities}.
I don't know if your link that you shared with me, explian that, but I will take a look.
If you have more documents that will help me to comprehend that can you send to me?

Thanks again for your help.
Rafael 
DeveloperSudDeveloperSud
Hi Rafael,

Please follow the link I shared with you,You will come to know.You can search for the resources like how you can display controller variables and methods in visualforce .You can take help from trailhead excercises.Thanks!!!.