+ Start a Discussion
PrIy@nK@PrIy@nK@ 

Accessing Javascript variable in visualforce component

Hello

I have been trying to work in Professional edition where apex classes are not supported. Here is a currency field in opportunities called "recent sale". So, here I need to create a visualforce page and use it to create a dashboard which should show the percentage of total of "recent value" field of all the opportunity records having status as "Closed Won" against the total of "recent value" field of all the opportunity records.

For example, if I am having 100 opportunity records, each having "recent value" as $10.0, and having 25 records among those with status as "Closed won", then the percentage needed here is (25*10)*100/100*10. i.e. the percentage of total "recent value" of "Closed Won" opportunities only.

So, I have been tryin to create a VF for this purpose. Following is the code I am using:

<apex:page showHeader="false" sidebar="false" standardStylesheets="true">


<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>

<script type="text/javascript">
    function initPage() {
          sforce.connection.sessionId = '{!$Api.Session_ID}';
          var closedWonOpp = sforce.connection.query("SELECT StageName, Most_Recent_Value__c FROM Opportunity where StageName = 'Closed Won'");
          var Opp = sforce.connection.query("SELECT StageName, Most_Recent_Value__c FROM Opportunity");
  
          var msvcw=0;
          var msvopp=0;
          for (var i=0; i<closedWonOpp.records.length; i++){
                 msvcw=msvcw + parseInt(closedWonOpp.records[i].get("Recent_Value__c"));         
          }
   
         for (var i=0;i<Opp.records.length;i++){
                 msvopp=msvopp+ parseInt(Opp.records[i].get("Recent_Value__c"));
          }
  
         var p=0;
         var p=msvcw*100/msvopp;
         alert("Recent value ercentage of closed won records is :" +p+"%");

     }

  </script>
 
    <apex:form >
        <apex:commandButton value="click" onclick="initPage();"/>
    </apex:form>  
</apex:page>


On click of the button, I am able to get all the correct percentage value in the dialogue box. But not able to figure out how to put them into the visualforce page and create a dashboard.

NOTE: Apex classes can't be used.

Jim JamJim Jam
You can try this (see items in bold)..

Basically, you reference a DOM element in your Javascript and update the HTML.

<apex:page showHeader="false" sidebar="false" standardStylesheets="true">


<script src="/soap/ajax/9.0/connection.js" type="text/javascript"></script>

<script type="text/javascript">
    function initPage() {
          sforce.connection.sessionId = '{!$Api.Session_ID}';
          var closedWonOpp = sforce.connection.query("SELECT StageName, Most_Recent_Value__c FROM Opportunity where StageName = 'Closed Won'");
          var Opp = sforce.connection.query("SELECT StageName, Most_Recent_Value__c FROM Opportunity");
 
          var msvcw=0;
          var msvopp=0;
          for (var i=0; i<closedWonOpp.records.length; i++){
                 msvcw=msvcw + parseInt(closedWonOpp.records[i].get("Recent_Value__c"));        
          }
  
         for (var i=0;i<Opp.records.length;i++){
                 msvopp=msvopp+ parseInt(Opp.records[i].get("Recent_Value__c"));
          }
 
         var p=0;
         var p=msvcw*100/msvopp;
         alert("Recent value ercentage of closed won records is :" +p+"%");
         
         output1.innerHTML=p;

     }

  </script>

    <apex:form >
        <apex:commandButton value="click" onclick="initPage(); return false;"/>
        <apex:outputText id="output1" />       
        <script>
            var output1 = document.getElementById("{!$Component.output1}");
        </script>

    </apex:form> 
</apex:page>
sunny522sunny522
Hi Priyanka,
         Go through the example  in the link given below.
 http://salesforceglobe4u.blogspot.in/2014/06/how-to-do-mathematical-functions-in.html

if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.