4

I am trying to use a public variable in a global static variable. However, it is giving an error: *Compile Error: Variable does not exist: dist *

Here is the controller code:

global with sharing class UserStatsController {


public string regn {get; set;}
public string dist {get; set;}    

public list <SelectOption> getValuesr()
{
    list <selectoption> lsreg = new List<selectoption> ();
    lsreg.add(new SelectOption('', '--None--'));
    Set<String> regionSet = new Set<String>();
    for (User_Stats__C Rgn : [SELECT Region__c FROM User_Stats__C where Region__c != null and Region__c != 0 and Region__c != 1 and District__c != null and District__c != '0'])
    {
        String region = string.valueOf(Rgn.Region__c);
        if(!regionSet.contains(region)) lsreg.add(new selectoption(region, region));
        regionSet.add(region);
    }
    return lsreg;
}

public list <SelectOption> getValues()
{
    list <selectoption> ls = new List<selectoption> ();
    ls.add(new SelectOption('', '--None--'));
    Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;
    Set<String> distSet = new Set<String>();
    for (User_Stats__C dist : [SELECT District__c FROM User_Stats__c where Region__c =: regnInt and District__c != null and Region__c != null and Region__c != 1 and Region__c != 0])
    {
        if(!distSet.contains(dist.District__c)) ls.add(new selectoption(dist.District__c, dist.District__c));
        distSet.add(dist.District__c);
    }
    return ls;
}
@RemoteAction  
global static User_Stats__c[] loadOpps(string dist, string regn) {
        Integer regnInt = (regn!=null) ? Integer.valueOf(regn) : -1;  
    return [SELECT Sales_Rep__c, District__c, Region__c, Rental_Revenue_Actual__c, Rental_Revenue_Goal__c FROM User_Stats__C where Region__c =: regnInt and District__c =: dist and Rental_Revenue_Actual__c != null and Rental_Revenue_Actual__c != 0 and Rental_Revenue_Goal__c != null and Rental_Revenue_Goal__c != 0 limit 20];
} 
}

Following is the VF page:

<apex:page Controller="UserStatsController" sidebar="false" showHeader="false">
<apex:includeScript id="a" value="https://www.google.com/jsapi" />
<apex:sectionHeader title="Goal Revision Dashboard" subtitle="District View"/>
<apex:pageBlock >

<apex:form >
 <apex:outputLabel value="Region" />
    <apex:selectList value="{!regn}" size="1" id="Region">
        <apex:selectOptions value="{!Valuesr}" />
        <apex:actionSupport event="onchange" reRender="districtSelect"/>
    </apex:SelectList>

    <br> </br>
    <apex:outputPanel id="districtSelect">
    <apex:outputLabel value="District"/>
    <apex:selectList value="{!dist}" size="1" id="dist">
        <apex:selectOptions value="{!Values}"/>
        <apex:actionSupport event="onchange" reRender="datarefresh,piegraph,bargraph,Num_OSR,OSR_Rev"/>
    </apex:selectList>
    </apex:outputPanel>
    <apex:pageBlock >
    <div id="chartBlock" />
<script type="text/javascript">
    google.load('visualization', '1.0', {'packages':['corechart']});
    google.setOnLoadCallback(initCharts);
    function initCharts() {        
      UserStatsController.loadOpps(
             function(result, event){ 
                 var visualization = new google.visualization.ColumnChart(document.getElementById('chartBlock'));
                 var data = new google.visualization.DataTable();
                 data.addColumn('string', 'Sales_Rep__c');
                 data.addColumn('number', 'Rental_Revenue_Actual__c');
                 data.addColumn('number', 'Rental_Revenue_Goal__c');   
                 for(var i =0; i<result.length;i++){
                    var r = result[i];
                    data.addRow([r.Sales_Rep__c, r.Rental_Revenue_Actual__c, r.Rental_Revenue_Goal__c]);
                  }
                visualization.draw(data, {legend : {position: 'top', textStyle: {color: 'blue', fontSize: 10}}, width:window.innerWidth,vAxis:{textStyle:{fontSize: 10}},hAxis:{textStyle:{fontSize: 10},showTextEvery:1,slantedText:false}});
          }, {escape:true});
      }
</script>
</apex:pageBlock>    
</apex:page>

Please help!

4

This is an instance variable, he will not be visible from static methods:

public string dist {get; set;}

@RemoteAction annotated methods are necessarily static, and class properties won't populate from viewstate or accessors in your page. I'd recommend handing it into the remote action invocation:

Server side change:

@RemoteAction global static User_Stats__c[] loadOpps(String dist) {
    return [
        SELECT
            Sales_Rep__c,
            District__c,
            Region__c,
            Rental_Revenue_Actual__c,
            Rental_Revenue_Goal__c
        FROM User_Stats__C
        WHERE District__c = :dist
        AND Rental_Revenue_Actual__c != null
        AND Rental_Revenue_Actual__c != 0
        AND Rental_Revenue_Goal__c != null
        AND Rental_Revenue_Goal__c != 0
        LIMIT 20
    ];
}

Client side change:

//this finds the dropdown by the correct id, eg 'j_id0:j_id2:j_id3:dist'
var dist = document.getElementById('{!$Component.dist}').value;

//and passes it into the @RemoteAction invocation as the first argument
UserStatsController.loadOpps(dist, function() {
...
8
  • Thanks @user320. The error is gone. However, the district variable holds value of a selectlist. When I change the values of dist in selectlist, it should refresh the data in above select statemement. However, that is not happening. Any reason why so? Thanks again – Robin Jan 8 '14 at 15:35
  • @RemoteAction are blind to any and all bound variables on pages, including your list. I think you'll need to update your JavaScript to fish out the value of your selectlist from the DOM and hand it wherever you call Visualforce.remoting.Manager.invokeAction - – Matt and Neil Jan 8 '14 at 15:41
  • I am very new to visualforce so I could not understand what you meant by update my JavaScript. If you can give an example, that would be most helpful. Thanks – Robin Jan 8 '14 at 16:03
  • I have updated the code – Robin Jan 8 '14 at 16:47
  • Thanks @Robin - now can you see the two lines to amend in the JavaScript? – Matt and Neil Jan 8 '14 at 17:12
1

The problem is that dist is an instance variable and your method is static. Because there is no instance when the code runs, the variable does not exist. Simply make dist static!

1
  • 1
    This will compile, but not sure it'd have the desired effect: how will dist get any value ever, if the entry point is a @RemoteAction invocation? – Matt and Neil Jan 8 '14 at 15:30

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.