+ Start a Discussion
Greg MGreg M 

Need help getting list of opportunities on VFP

I am having problems getting the VFP to show opportunities that are filtered. I am looking to recreate a summary report (the reason why isn't important to the question).

I have a string/text field Close_Month_Forecasted__c (close month) and a string/text field Account.Terr_From_Zip__c (territory) field on the opportunity object. I am trying to put/show the opportunity records that match not only the close month but the territory (Account.Terr_From_Zip__c)

Can anyone provide some assistance?
public class ForecastController {
public Map<String, Map<String, List<Opportunity>>> values {get;set;}
public List<ForecastWrapper> forecasts { get; set; }

public class ForecastWrapper {
    public String month {get;set;}
    public String[] territories {get;set;}

public void load(){

    // Load all the data into a map
    Map<String, Map<String, List<Opportunity>>> values = new Map<String, Map<String, List<Opportunity>>>();

    for(Opportunity record:
        [SELECT Id, Name, Account.Name, Account.Territory_Manager__c, StageName, CloseDate, Forecastable__c, 
         Forecasted_Close_Date_30_60_90__c, Close_Month_Forecasted__c, Account.Terr_From_Zip__c, 
         (Select Id, Account_Name__c, ADRBD_Forecast_Close_Date__c, Close_Date__c, Close_Month_Forecasted__c, Install_Date_Instruments_only__c, Opportunity__c, 
          Opportunity_Name__c, Opp_Safe_ID__c, Quantity__c, Stage__c, Territory__c, Territory_Manager__c, Total_FS_Modules__c, Total_Instr_Revenue__c, Total_Price__c, CreatedDate FROM Opportunity_Snapshots__r)
         FROM Opportunity    
         WHERE (NOT Name LIKE 'Test')
         AND (Account.BillingCountryCode = 'US' OR Account.BillingCountryCode = 'CA')
         AND Forecastable__c = true
         AND Forecasted_Close_Date_30_60_90__c <> null
         ORDER BY Account.Name
        ]) {

            Map<String, List<Opportunity>> dateRange = values.get(record.Close_Month_Forecasted__c);

            if(dateRange == null) {
                values.put(record.Close_Month_Forecasted__c, dateRange = new Map<String, List<Opportunity>>());
            List<Opportunity> territoryList = dateRange.get(record.Account.Terr_From_Zip__c);
            if(territoryList == null) {
                dateRange.put(record.Account.Terr_From_Zip__c, territoryList = new List<Opportunity>());

    forecasts = new ForecastWrapper[0];

    // Load the "keys" into our specified order
    String[] months = new List<String>(values.keySet());

    for(String month: months) {
        Map<String, List<Opportunity>> territoryList = values.get(month);
        String[] territories = new List<String>(territoryList.keySet());
        ForecastWrapper wrap = new ForecastWrapper();
        wrap.month = month;
        wrap.territories = territories;
<apex:page controller="ForecastController" action="{!load}" sidebar="false"  tabStyle="Opportunity" docType="html-5.0">

        table {
        border-collapse: collapse;
        width: 100%;


        th {
        text-align: left;
        padding: 4px;


        td {
        text-align: left;
        padding: 4px;
        <!--border-right: 1px solid black;-->

        tr:nth-child(even) {background-color: #cccccc;}

    <apex:sectionHeader title="Opportunities" subtitle="Forecast Review"/>
            <th>Account Name</th>
            <th>Territory Manager</th>
            <th>Prev Stage</th>
            <th>Opportunity Name</th>

    <apex:repeat value="{!forecasts}" var="c" >
        <apex:pageBlock title="Close Date (Forecasted) {!c.month}" >
            <apex:repeat value="{!c.territories}" var="t" >
                <apex:pageBlock title="Territory {!t}" >
                    <!-- access map variables -->
                    <apex:repeat var="opps" value="{!values[c][t]}" >
                            <td> {!opps.Account.Name} </td>
                            <td> {!opps.Account.Territory_Manager__c} </td>
                                <apex:outputText rendered="{!opps.Opportunity_Snapshots__r.size > 0}">
                                    <!-- {!opps.Opportunity_Snapshots__r[0].Stage__c} -->


User-added image