如何从Visualforce页面向Salesforce控制器传递静态硬编码值
Solution: Reuse Single Controller with Static WHERE Clause Parameters for Visualforce Pages
Hey there! With your Python background, this will make sense quickly—we just need to adjust how your Visualforce (VF) pages pass that static StageVal to the controller, and fix a couple of timing/formatting issues in your original code. Here's a step-by-step implementation:
Key Fixes & Concepts First
- Timing Issue: Your original controller calls
getData()in the constructor, which runs before VF page parameters are bound to the controller properties. We'll move data fetching to anactionmethod that runs after parameter binding. - SOQL Safety & Syntax: Always use variable binding (like
:StageVal) instead of string concatenation for dynamic values, and fix date formatting (wrap dates in single quotes). - Static Parameter Passing: We'll use
apex:paramto hardcode the uniqueStageValdirectly in each VF page.
Step 1: Updated Controller Code
public class SalesRev_UpFrontFees { public String StageVal {get; set;} public Integer salesVal {get; set;} // Made this a property for easier VF access public SalesRev_UpFrontFees() { // Initialize default value to avoid nulls salesVal = 0; } // Action method to fetch data after StageVal is set by VF page public PageReference fetchData() { if(String.isBlank(StageVal)) { salesVal = 0; return null; } // Fixed SOQL syntax (dates in quotes, proper variable binding) String soqlString = 'SELECT SUM(Total_Subscription_Fees__c) RevSum ' + 'FROM Opportunity ' + 'WHERE CloseDate >= \'2018-01-01\' AND CloseDate <= \'2018-12-31\' ' + 'AND RecordType.Name = \'Enterprise Opportunity\' ' + 'AND StageName = :StageVal'; AggregateResult[] groupedResults = Database.query(soqlString); // Handle empty result set to avoid index out of bounds if(groupedResults != null && !groupedResults.isEmpty()) { Decimal revSum = (Decimal)groupedResults[0].get('RevSum'); salesVal = revSum != null ? revSum.intValue() : 0; } else { salesVal = 0; } return null; // Stay on the same page } }
Step 2: Updated Visualforce Page (Example for "Closed Won" Stage)
Each of your 8 VF pages will be identical except for the value attribute in apex:param (replace 'Closed Won' with your specific stage name for each page):
<apex:page controller="SalesRev_UpFrontFees" action="{!fetchData}"> <head> <apex:includeScript value="{!URLFOR($Resource.VFGauge, 'VFGauge/jQuery/jquery-1.10.2.min.js')}" /> <apex:includeScript value="{!URLFOR($Resource.VFGauge, 'VFGauge/jQuery/jquery-ui-1.10.2.custom.min.js')}" /> <apex:includeScript value="{!URLFOR($Resource.Raphael, 'Raphael/resources/js/raphael.2.1.0.min.js')}" /> <apex:includeScript value="{!URLFOR($Resource.Raphael, 'Raphael/resources/js/justgage.1.0.1.js')}" /> <apex:stylesheet value="{!URLFOR($Resource.Raphael, 'Raphael/styles/style.css')}"/> </head> <!-- Pass static StageVal to controller BEFORE fetchData runs --> <apex:param name="StageVal" value="Closed Won" assignTo="{!StageVal}" /> <apex:form id="myForm"> <apex:pageBlock id="xxx"> <div id="gauge1" style="width:320px; height:256px"></div> <script type="text/javascript"> var g1 = new JustGage({ id: "gauge1", value: {!salesVal}, min: 0, max: 1000000000, title: "WW Sales Revenue", levelColorsGradient: false, gaugeWidthScale: 0.4, valueFontSize: "5px" }); </script> <a id="linear-gauge" href=""></a> </apex:pageBlock> </apex:form> </apex:page>
How This Works
- Static Parameter Binding: The
apex:paramtag in each VF page hardcodes the uniqueStageValand assigns it directly to the controller'sStageValproperty. - Timing: The
action="{!fetchData}"in theapex:pagetag runs after theapex:paramvalues are bound to the controller, soStageValis already set whenfetchData()executes. - Reusability: Just copy the VF page 7 more times, changing only the
valueinapex:paramto match each stage you need (e.g.,'Closed Lost','Proposal Sent', etc.).
Alternative Approach: Use Page Parameters
If you prefer, you can also pass the static value via the page's URL parameters (though the apex:param method is cleaner for static values). For this:
- Remove the
apex:paramfrom the VF page - In the controller's
fetchData()method, get the parameter directly:StageVal = ApexPages.currentPage().getParameters().get('StageVal'); - Then, each VF page's URL would include
?StageVal=Closed+Won, but you can hardcode this in the page's deployment or link.
内容的提问来源于stack exchange,提问作者Theway
相关产品推荐
相关产品推荐

