+ Start a Discussion
Greg MGreg M 

Need Help with Custom Controller & VF Page Please

Can anyone provide insight into what I need to do to prevent my page from displaying Territories that return zero results?

They are currently showing because I made a list and my VF page loops through all “Close Date (Forecasted)” fields  in the list using apex:repeat. Then an inner apex:repeat loops through all territories in the territory list.

The problem is that there are not always opportunities that are in both lists. This causes 
******** CONTROLLER *******

public class ForecastController {
    
    public List<Opportunity> opportunities {get;set;}
    public String[] closeDates {get;set;}
    public String[] terrs {get;set;}
    
    
    public void load(){
        
        Opportunities = [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
                        ];
        
        // Creating Set to store unique values
        Set<String> monthSet = new Set<String>();
        Set<String> terrSet = new Set<String>();
        for (Opportunity o : opportunities){
            monthSet.add(o.Close_Month_Forecasted__c);
            terrSet.add(o.Account.Terr_From_Zip__c);
        }
        
        // Converting to List in order to sorth the results alphabetically
        List<String> monthList = new List<String>(monthSet);
        List<String> terrList = new List<String>(terrSet);
        monthList.sort();
        terrList.sort();
        
        
        closeDates = new String[monthList.size()];
        Integer i = 0;
        for(String month : monthList){
            closeDates[i] = month;
            i++;
            
        }
        
        terrs = new String[terrList.size()];
        Integer i2 = 0;
        for(String terr : terrList){
            terrs[i2] = terr;
            i2++;
            
        }
        
    }
    
}




**** VF PAGE ****



<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"/>
    <apex:repeat value="{!closeDates}" var="c" >
        
        <apex:pageBlock title="Close Date (Forecasted) {!c}" >
            
            <apex:repeat value="{!terrs}" var="t" >
                <apex:pageBlock title="Territory {!t}" >
                    <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>
                        
                        <apex:repeat var="opps" value="{!opportunities}">
                            
                            <apex:variable var="v" value="" rendered="{!IF(c=opps.Close_Month_Forecasted__c && t=opps.Account.Terr_From_Zip__c,true,false)}">
                                
                                <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="{!IF(opps.Opportunity_Snapshots__r.size > 0,true,false)}">
                                            {!opps.Opportunity_Snapshots__r[0].Stage__c}
                                        </apex:outputText>
                                    </td>
                                    
                                    <td>{!opps.Name}</td>
                                    
                                </tr>
                                
                            </apex:variable>
                            
                        </apex:repeat> 
                        
                    </table>
                </apex:pageBlock>
            </apex:repeat>
        </apex:pageBlock>
        
    </apex:repeat>
    
</apex:page>

User-added image
Christan G 4Christan G 4
Hi Greg, I hope you are well. For the line that states <apex:pageBlock title="Territory {!t}" >, I think we'll have to use the rendered attribute and  associate it with a formula expression. If it returns true, then it'll show on the page if it returns false then it won't.

I was thinking of something like <apex:pageBlock title="Territory {!t}"  rendered="{!If(NOT(ISNULL(CONDITIONAL_CRITERIA)),'true','false')}">

The rendering attribute should control which territory pageblock is shown.

The part I am unsure about is what the conditional criteria should be.
Greg MGreg M
Hey Christan, I appreciate the reply. Yeah I am not sure what the solution is either.