我有一个visualForce页面和一个顶点控制器。我需要从我的visualForce页面获取一个日期值到我的Apex控制器(这已经完成),然后将此值传递给我的SOQL查询。当我将实际日期值放入时,我的SOQL查询运行正常,但是当我使用创建的变量之前,它说找不到名为x的变量。
我确定这是一个普通的编程错误,这只是我第二周使用代码而不是点击!
如果您想要查看VF页面,请告诉我。
** Apex Controller ***
global with sharing class GoogleChartsController2 {
Opportunity o = new Opportunity();
public Opportunity getProxyObject() {
return o;
}
/**
Loads all Opportunities and then filters
*/
@RemoteAction
global static Opportunity[] loadOpps() {
return [select Id, Name, ExpectedRevenue, LeadSource, DaysToStartDate__c, Probability, CloseDate, Amount from Opportunity WHERE CloseDate = :o.CloseDate ];
}
}
来自VisualForce页面的部分 *
<apex:page controller="GoogleChartsController2" sidebar="false">
<!-- Google API inclusion -->
<apex:includescript id="a" value="https://www.google.com/jsapi">
<apex:sectionheader title="Google Charts + Javascript Remoting" subtitle="Demoing - Opportunities by Exepected Revenue">
<!-- Google Charts will be drawn in this DIV -->
<div id="chartBlock" style="width: 900px; height: 500px;">>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(initCharts);
function initCharts() {
// Following the usual Remoting syntax
// [<namespace>.]<controller>.<method>([params...,] <callbackFunction>(result, event) {...}
// controller : GoogleChartsController
// method : loadOpps
GoogleChartsController2.loadOpps(
function(result, event){
// load Column chart
var visualization = new google.visualization.BubbleChart(document.getElementById('chartBlock'));
// Prepare table model for chart with columns
var data = new google.visualization.DataTable();
data.addColumn('string', 'Opportunity');
data.addColumn('number', 'Days To Contract Start');
data.addColumn('number', 'Probability (%)');
data.addColumn('string', 'Lead Source');
data.addColumn('number', 'Amount');
// add rows from the remoting results
for(var i =0; i<result.length;i++){
var r = result[i];
data.addRow([r.Name, r.DaysToStartDate__c, r.Probability, r.LeadSource, r.Amount]);
}
// all done, lets draw the chart with some options to make it look nice.
visualization.draw(data, {
legend : {position: 'right', textStyle: {color: 'black', fontSize: 10}},
width:window.innerWidth,
length:window.innerLength,
vAxis:{title: 'Probability', textStyle:{fontSize: 10}, minValue: 0, maxValue: 100},
hAxis:{title: 'Days Until Close' , textStyle:{fontSize: 10},
chartArea: {width: '50%', height: '75%'},
sizeAxis: {minSize: 150, minValue: 150},
showTextEvery:1,slantedText:false}})
},
{escape:true});
}
</script>
</div></apex:sectionheader></apex:includescript>
<apex:form >
<apex:outputlabel value="Enter your name here"/>
<apex:inputField value="{!proxyObject.closeDate}"/>
<apex:actionsupport event="onclick" rerender="display" />
<apex:outputpanel id="display">
<apex:outputtext value="The date entered is {!proxyObject.closeDate}"/>
</apex:outputpanel>
</apex:form>
</apex:page>
答案 0 :(得分:1)
对远程处理的需求使这更有趣。因为远程处理方法必须声明为静态(就像您已经完成的那样),所以您将无法引用您在控制器中使用的任何成员变量。但是,您可以在远程调用中从页面传递其值。
首先,您需要修改远程方法以接受来自调用它的Javascript中的参数。然后,您将在SOQL查询中解析该日期字符串。您可以将proxyObject
保留在控制器中,以便利用Salesforce提供的现成日期选择器,但您不会直接在loadOpps
方法中引用该变量。 / p>
global with sharing class GoogleChartsController2 {
public Opportunity proxyObject {public get; public set;}
public GoogleChartsController2() {
proxyObject = new Opportunity();
// just giving this a value for testing purposes
proxyObject.CloseDate = System.today();
}
/** Loads all Opportunities and then filters */
@RemoteAction
global static Opportunity[] loadOpps(String closedate) {
return [select Id, Name, ExpectedRevenue, LeadSource, Probability, CloseDate, Amount from Opportunity WHERE CloseDate = :Date.parse(closedate)];
}
}
接下来,在调用loadOpps
时,您必须在VF页面上引用该字段。您可以通过几种不同的方式执行此操作,对于这些情况,最简单的方法可能是使用VF $Component
合并字段标记。这将要求您为嵌套的VF标记指定ID:
<apex:form id="testForm" >
<apex:inputField id="closedate" value="{!proxyObject.closeDate}"/>
然后可以这样引用:
var dt = document.getElementById("{!$Component.testForm.closedate}").value;
所以你的最终VF页面会变成:
<apex:page controller="GoogleChartsController2" sidebar="false">
<apex:sectionheader title="Google Charts + Javascript Remoting" subtitle="Demoing - Opportunities by Expected Revenue" />
<apex:form id="testForm" >
<apex:outputlabel value="Enter a date here"/>
<apex:inputField id="closedate" value="{!proxyObject.closeDate}" >
<apex:actionsupport event="onchange" rerender="display" />
</apex:inputField>
<apex:outputpanel id="display">
<apex:outputtext value="The date entered is {!proxyObject.closeDate}"/>
</apex:outputpanel>
</apex:form>
<!-- Google API inclusion -->
<apex:includescript id="a" value="https://www.google.com/jsapi">
<!-- Google Charts will be drawn in this DIV -->
<div id="chartBlock" style="width: 900px; height: 500px;">>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(initCharts);
function initCharts() {
// Following the usual Remoting syntax
// [<namespace>.]<controller>.<method>([params...,] <callbackFunction>(result, event) {...}
// controller : GoogleChartsController
// method : loadOpps
var dt = document.getElementById("{!$Component.testForm.closedate}").value;
GoogleChartsController2.loadOpps( dt,
function(result, event){
// load Column chart
var visualization = new google.visualization.BubbleChart(document.getElementById('chartBlock'));
// Prepare table model for chart with columns
var data = new google.visualization.DataTable();
data.addColumn('string', 'Opportunity');
data.addColumn('number', 'Days To Contract Start');
data.addColumn('number', 'Probability (%)');
data.addColumn('string', 'Lead Source');
data.addColumn('number', 'Amount');
// add rows from the remoting results
for(var i =0; i<result.length;i++){
var r = result[i];
data.addRow([r.Name, r.DaysToStartDate__c, r.Probability, r.LeadSource, r.Amount]);
}
// all done, lets draw the chart with some options to make it look nice.
visualization.draw(data, {
legend : {position: 'right', textStyle: {color: 'black', fontSize: 10}},
width:window.innerWidth,
length:window.innerLength,
vAxis:{title: 'Probability', textStyle:{fontSize: 10}, minValue: 0, maxValue: 100},
hAxis:{title: 'Days Until Close' , textStyle:{fontSize: 10},
chartArea: {width: '50%', height: '75%'},
sizeAxis: {minSize: 150, minValue: 150},
showTextEvery:1,slantedText:false}})
},
{escape:true});
}
</script>
</div></apex:includescript>
</apex:page>
这似乎对我来说很有用,虽然图表API抱怨第2列是一个字符串......但是你应该能够通过改变你的列来解决这个问题。请注意,每当您更改日期时,这都不会重新呈现图表,但如果您对apex:actionSupport
代码进行一些更改,这应该不会很困难。
希望这能让你走上正轨!