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
Uday MenkurkarUday Menkurkar 

Can you have multiple instantiations of StandardController??

Is it possible to have multiple instantiations of a StandardController?  For example, in a VF page below I would like to display two types of lists.  Deals which with "High Likelihood" and deals with "Low Likelihood".  And for each Deal I'm also displaying associated child records.  Currently I've only one query in the extension controller "extCon".  What I would like to do is split the query into two queries so I can add a WHERE clause to both and show two separate page blocks in the same page.  Is it possible to have two separate controller instantiations?  Or, is there better way to accomlish the same task??

<!-- Visualforce code -->
<apex:page standardController="PP_Deals__c" recordSetVar="deals" extensions="extCon">

    <font size="5" color="blue"> <b> Private Placement - Active Deal Log1 </b> </font>
   
    <apex:pageBlock title="Higher Chance">
        <apex:pageBlockTable value="{! deals }" var="ds">
            <apex:column value="{! ds.Issuer__c }"/>
            <apex:column headerValue="Bids Due" value="{! ds.Circle_Due_Date__c }"/>
            <apex:column headerValue="Maturities">           
                    <apex:pageBlockTable value="{!ds.Tranches__r}" var="val">
                        <apex:column headerValue="Tenor" value="{! val.name }"/>
                  </apex:pageBlockTable>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
   
    <apex:pageBlock title="Lower Chance">
        <apex:pageBlockTable value="{! deals }" var="ds">
            <apex:column value="{! ds.Issuer__c }"/>
            <apex:column headerValue="Bids Due" value="{! ds.Circle_Due_Date__c }"/>
            <apex:column headerValue="Maturities">           
                    <apex:pageBlockTable value="{!ds.Tranches__r}" var="val">
                        <apex:column headerValue="Tenor" value="{! val.name }"/>
                  </apex:pageBlockTable>
            </apex:column>
 
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

And, here is the extension controller code.  Here I'm wondering if it is possible to have two separate initiations so I can add a WHERE clause??  And, if yes, how to call two separate initiations in VF above??

public class extCon {
    private final PP_Deals__c deal;
  
    public extCon(ApexPages.StandardSetController controller) { 
               controller = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, Name FROM PP_Deals__c]));
    }
}

Thanks
Best Answer chosen by Uday Menkurkar
Glyn Anderson 3Glyn Anderson 3
Do you have a reason to use the StandardController?  Instead of having a controller extension, you could just use a custom controller.  Create a custom controller that queries the deal records and creates two lists.  Render one list in one page block and the other list in the other page block.

<pre>
public class DealController
{
    public DealController()
    {
        // nothing to do here
    }

    // use whatever parameters you need to filter the query properly
    private List<PP_Deals__c> queryDeals( String likelihood )
    {
        return
        [   SELECT  Id, Issuer__c, Circle_Due_Date__c,
                (   SELECT  Id, Name
                    FROM    Tranches__r
                )
            FROM    PP_Deals__c
            WHERE   Likelihood__c = :likelihood
        ];
    }

    public List<PP_Deals__c> highLikelihoodDeals
    {
        get
        {
            if ( highLikelihoodDeals == null )
            {
                highLikelihoodDeals = queryDeals( 'High Likelihood' );
            }
            return highLikelihoodDeals;
        }
        private set;
    }

    public List<PP_Deals__c> lowLikelihoodDeals
    {
        get
        {
            if ( lowLikelihoodDeals == null )
            {
                lowLikelihoodDeals = queryDeals( 'Low Likelihood' );
            }
            return lowLikelihoodDeals;
        }
        private set;
    }
}

<apex:page controller="DealController" >

    <font size="5" color="blue"> <b> Private Placement - Active Deal Log1 </b> </font>

    <apex:pageBlock title="Higher Chance">
        <apex:pageBlockTable value="{! highLikelihoodDeals }" var="ds">
            <apex:column value="{! ds.Issuer__c }"/>
            <apex:column headerValue="Bids Due" value="{! ds.Circle_Due_Date__c }"/>
            <apex:column headerValue="Maturities">           
                    <apex:pageBlockTable value="{!ds.Tranches__r}" var="val">
                        <apex:column headerValue="Tenor" value="{! val.name }"/>
                  </apex:pageBlockTable>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>

    <apex:pageBlock title="Lower Chance">
        <apex:pageBlockTable value="{! lowLikelihoodDeals }" var="ds">
            <apex:column value="{! ds.Issuer__c }"/>
            <apex:column headerValue="Bids Due" value="{! ds.Circle_Due_Date__c }"/>
            <apex:column headerValue="Maturities">           
                    <apex:pageBlockTable value="{!ds.Tranches__r}" var="val">
                        <apex:column headerValue="Tenor" value="{! val.name }"/>
                  </apex:pageBlockTable>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
</pre>

All Answers

Glyn Anderson 3Glyn Anderson 3
Do you have a reason to use the StandardController?  Instead of having a controller extension, you could just use a custom controller.  Create a custom controller that queries the deal records and creates two lists.  Render one list in one page block and the other list in the other page block.

<pre>
public class DealController
{
    public DealController()
    {
        // nothing to do here
    }

    // use whatever parameters you need to filter the query properly
    private List<PP_Deals__c> queryDeals( String likelihood )
    {
        return
        [   SELECT  Id, Issuer__c, Circle_Due_Date__c,
                (   SELECT  Id, Name
                    FROM    Tranches__r
                )
            FROM    PP_Deals__c
            WHERE   Likelihood__c = :likelihood
        ];
    }

    public List<PP_Deals__c> highLikelihoodDeals
    {
        get
        {
            if ( highLikelihoodDeals == null )
            {
                highLikelihoodDeals = queryDeals( 'High Likelihood' );
            }
            return highLikelihoodDeals;
        }
        private set;
    }

    public List<PP_Deals__c> lowLikelihoodDeals
    {
        get
        {
            if ( lowLikelihoodDeals == null )
            {
                lowLikelihoodDeals = queryDeals( 'Low Likelihood' );
            }
            return lowLikelihoodDeals;
        }
        private set;
    }
}

<apex:page controller="DealController" >

    <font size="5" color="blue"> <b> Private Placement - Active Deal Log1 </b> </font>

    <apex:pageBlock title="Higher Chance">
        <apex:pageBlockTable value="{! highLikelihoodDeals }" var="ds">
            <apex:column value="{! ds.Issuer__c }"/>
            <apex:column headerValue="Bids Due" value="{! ds.Circle_Due_Date__c }"/>
            <apex:column headerValue="Maturities">           
                    <apex:pageBlockTable value="{!ds.Tranches__r}" var="val">
                        <apex:column headerValue="Tenor" value="{! val.name }"/>
                  </apex:pageBlockTable>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>

    <apex:pageBlock title="Lower Chance">
        <apex:pageBlockTable value="{! lowLikelihoodDeals }" var="ds">
            <apex:column value="{! ds.Issuer__c }"/>
            <apex:column headerValue="Bids Due" value="{! ds.Circle_Due_Date__c }"/>
            <apex:column headerValue="Maturities">           
                    <apex:pageBlockTable value="{!ds.Tranches__r}" var="val">
                        <apex:column headerValue="Tenor" value="{! val.name }"/>
                  </apex:pageBlockTable>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
</pre>
This was selected as the best answer
Uday MenkurkarUday Menkurkar
Glyn,  thank you so much for a detailed working example.  It works beautifully!!  Don't have enough words to thank you.  Terrific support of the developer community is out of this world!!