function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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>());
            }
            territoryList.add(record);
        }

    forecasts = new ForecastWrapper[0];

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

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

        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;}
    </style>

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

    <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]}" >
                        <tr>
                            <td> {!opps.Account.Name} </td>
                            <td>{!opps.Account.Terr_From_Zip__c}</td>
                            <td> {!opps.Account.Territory_Manager__c} </td>
                            <td>{!opps.StageName}</td>
                            <td>
                                <apex:outputText rendered="{!opps.Opportunity_Snapshots__r.size > 0}">
                                    <!-- {!opps.Opportunity_Snapshots__r[0].Stage__c} -->
                                </apex:outputText>
                            </td>
                            <td>{!opps.Name}</td>
                        </tr>
                    </apex:repeat> 
                </apex:pageBlock>
            </apex:repeat>
        </apex:pageBlock>
    </apex:repeat>

</apex:page>

User-added image