2

I have the following VF Page and Controller. The VF Page will have a dropdown of territories, and when that dropdown has a value selected, then I would like to render the PageBlockSections to show the correct SOQL query on Accounts to bring back the list of records from the controller.

It seems as if any choice in the dropdown calls the Else statement in my controller and always shows those records, and does not change upon selecting a new value, it always stays as the else records.

Can anyone help me get over the finish line here on this project to conditionally render the proper set of records on the VF page Blocks based on the selection of the drop down territory list on the VF page?

Thank you so much for any help you can provide!

Shawn

Controller Code -

public class ABMController {

    public SelectOption[] getSchedules() {
        return new SelectOption[] {new SelectOption('Value1', '--Select a Territory--'),
            new SelectOption('Value2', 'Northwest'), new SelectOption('Value3', 'Southwest'), new SelectOption('Value4', 'Lower South Central')};
    }

    public String TerritoryID {get; set;}
    public List<Account> accts {get; set;}
    public List<Account> unassigned {get; set;}
    User sapi = [SELECT Id,Name FROM User WHERE FirstName = 'SalesStaff' LIMIT 1];
    String TerrName = TerritoryID;


    public ABMController(){  

        if(TerritoryID == 'Value2'){
            accts = new List<Account>();
            unassigned = new List<Account>();
            accts = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Northwest' AND OwnerId !=: sapi.Id ORDER BY OwnerId ASC LIMIT 40];
            unassigned = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Northwest' AND OwnerId =: sapi.Id AND Status__c = 'Not Active' ORDER BY Average_TAM_Score__c DESC LIMIT 40];
            system.debug('The count for the accounts brought back for Northwest are:  ' + accts.size());
            system.debug('The count for the accounts brought back for Northwest are:  ' + unassigned.size());
        } Else If(TerritoryID == 'Value3') {
            accts = new List<Account>();
            unassigned = new List<Account>();
            accts = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Southwest' AND OwnerId !=: sapi.Id ORDER BY OwnerId ASC LIMIT 40];
            unassigned = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Southwest' AND OwnerId =: sapi.Id AND Status__c = 'Not Active' ORDER BY Average_TAM_Score__c DESC LIMIT 10];
            system.debug('The count for the accounts brought back for Southwest are:  ' + accts.size());
            system.debug('The count for the accounts brought back for Southwest are:  ' + unassigned.size());
        } else {
            accts = new List<Account>();
            unassigned = new List<Account>();
            accts = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Lower South Central' AND OwnerId !=: sapi.Id ORDER BY OwnerId ASC LIMIT 40];
            unassigned = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = 'Lower South Central' AND OwnerId =: sapi.Id AND Status__c = 'Not Active' ORDER BY Average_TAM_Score__c DESC LIMIT 40];
            system.debug('The count for the accounts brought back for Upper South Central are:  ' + accts.size());
            system.debug('The count for the accounts brought back for Upper South Central are:  ' + unassigned.size());
        } 



    }



    public PageReference showAccounts() {
        system.debug('EXECUTED');
        return null;
    }

    public PageReference save(){
        update accts;
        update unassigned;
        PageReference congratsPage = Page.ABMPage;
        congratsPage.setRedirect(true);
        return congratsPage;

    }


}

VF Page Code -

<apex:page controller="ABMController" showHeader="true" >
    <apex:form id="form">
        <h1></h1><br/>
        <apex:pageMessages />
        <apex:pageBlock mode="inlineEdit" id="Block1" >
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="SAVE"/>
            </apex:pageBlockButtons>

            <span id="idSpan">
            <apex:pageBlockSection title="ABM Assignment Tool" columns="1" id="Details">
                <apex:selectList value="{!TerritoryID}" size="1">
                    <apex:actionSupport event="onchange" reRender="wrapper1,wrapper2"/>
                    <apex:selectOptions value="{!schedules}"/>
                </apex:selectList>
            </apex:pageBlockSection>
            </span>

            <apex:outputPanel layout="none" id="wrapper1">
            <apex:pageBlockSection title="Assigned Accounts" collapsible="false" columns="5" rendered="{!TerritoryID != null}">
                <apex:pageBlockTable value="{!accts}" var="a" border="1">
                    <apex:column headervalue="Account Name">
                        <apex:outputLink value="/{!a.Id}">{!a.name}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Owner Name">
                        <apex:inputField value="{!a.ownerId}"/>
                    </apex:column>
                    <apex:column value="{!a.Armor_Territory__c}"/>
                    <apex:column value="{!a.Status__c}"/>
                    <apex:column value="{!a.Average_TAM_Score__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            </apex:outputPanel>


        <apex:outputPanel layout="None" id="wrapper2">
            <apex:pageBlockSection title="Un-Assigned Accounts" collapsible="false" columns="5" rendered="{!TerritoryID != null}" >
                <apex:pageBlockTable value="{!unassigned}" var="a2" border="1">
                    <apex:column headervalue="Account Name">
                        <apex:outputLink value="/{!a2.Id}">{!a2.name}</apex:outputLink>
                    </apex:column>
                    <apex:column headerValue="Owner Name">
                        <apex:inputField value="{!a2.ownerId}"/>
                    </apex:column>
                    <apex:column value="{!a2.Armor_Territory__c}"/>
                    <apex:column value="{!a2.Status__c}"/>
                    <apex:column value="{!a2.Average_TAM_Score__c}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            </apex:outputPanel>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Updated Controller code form sfdcFox';s advisement.

public class ABMController {

public SelectOption[] getSchedules() {
    return new SelectOption[] {new SelectOption('Value1', '--Select a Territory--'),
        new SelectOption('Value2', 'Northwest'), new SelectOption('Value3', 'Southwest'), new SelectOption('Value4', 'Lower South Central')};
}

public String TerritoryID {get; set;}
public List<Account> accts {get; set;}
public List<Account> unassigned {get; set;}
User sapi = [SELECT Id,Name FROM User WHERE FirstName = 'SalesStaff' LIMIT 1];


public ABMController(){  
    showAccounts();   
}



public void showAccounts() {
    String territory;
    switch on TerritoryID {
        when 'Value2' { territory = 'Northwest';}
        when 'Value3' { territory = 'Southwest';}
        when else { territory = 'Lower South Central';}
    }

    accts = [SELECT ID, Name, OwnerId, Armor_Territory__c, Status__c, Average_TAM_Score__c FROM Account WHERE Armor_Territory__c =:territory AND OwnerId !=: sapi.Id ORDER BY OwnerId ASC LIMIT 40];
    unassigned = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c =:territory AND OwnerId =: sapi.Id AND Status__c = 'Not Active' ORDER BY Average_TAM_Score__c DESC LIMIT 40];
    system.debug('The count for the accounts brought back for '+ territory +' are:  ' + accts.size());
    system.debug('The count for the accounts brought back for '+ territory +' are:  ' + unassigned.size());
}

public PageReference save(){
    update accts;
    update unassigned;
    PageReference congratsPage = Page.ABMPage;
    congratsPage.setRedirect(true);
    return congratsPage;

}


}
3

The constructor is only called once, when the page first loads. You need to put your logic in your action method:

public void showAccounts() {
    String territory;
    switch on TerritoryId {
      when 'Value2' { territory = 'Northwest'; }
      when 'Value3' { terrtiroy = 'Southwest'; }
      when else { territory = 'Lower South Central'; }
    }

    accts = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = :territory AND OwnerId !=: sapi.Id ORDER BY OwnerId ASC LIMIT 40];
    unassigned = [SELECT ID, NAME, OwnerId, Armor_Territory__c, Status__c,Average_TAM_Score__c FROM Account WHERE Armor_Territory__c = :territory AND OwnerId =: sapi.Id AND Status__c = 'Not Active' ORDER BY Average_TAM_Score__c DESC LIMIT 40];
    system.debug('The count for the accounts brought back for '+ territory +' are:  ' + accts.size());
    system.debug('The count for the accounts brought back for '+ territory +' are:  ' + unassigned.size());
}

This code shows a few standard optimizations: switch statements, using variables instead of copy-pasting code, not returning a PageReference if you do not intend to redirect, avoiding extra variable initialization, etc.

Your controller is now simplified to just:

public ABMController(){  
  showAccounts();
}

I just realized that the selection list isn't being reRendered, so it likely won't be submitted to the server. The following adjustment should work:

<apex:actionSupport event="onchange" reRender="idSpan,wrapper1,wrapper2"/>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.