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
ShivaShiva 

I want to use two controllers on the same page

Hi,

 

I have a situation where I need to display two different custom controllers on te same page.

 

1. First controller has one record.

2. Second controller has an array.

 

Thanks for your help in advance.

 

 

 

gtuerkgtuerk

Without seeing your code, it's hard to say if you even need two controllers; there is no reason why using a custom controller as an extension that you would not be able to pack any number of member variables (arrays, Custom objects, whatever) in that class.

 

<apex:page standardController="myObject__c" extensions="myCustomApexCode">

 {markup}

</apex:page>

 

remember that your extensions need to have a constructor that takes a StandardController or StandardSetController.

 

Post your code if this doesn't resonate

ShivaShiva

Thank you for your quick response. Please help me if you can. I am new to apex code.

 

 

<apex:page controller="MyController" tabStyle="cs_LicenseKey__c">
<apex:form >
    <apex:pageBlock title="Confirmation">
      <apex:pageBlockSection title="License Ky Information">
        <apex:outputField value="{!LicenseKey.Id}"/>
        <apex:outputField value="{!LicenseKey.name}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 


public class MyController {

        private final cs_LicenseKey__c LicenseKey;
       
        
        public LicenseKeyOptions()  {
       
        LicenseKeyOption[] aa = [select id,name from cs_LicenseKeyOption__c
                where LicenseKey__c = :ApexPages.currentPage().getParameters().get('id') LIMIT 1];
       
        }
        
       
        
        public MyController() {
            LicenseKey = [select id,name from cs_LicenseKey__c where id = :ApexPages.currentPage().getParameters().get('id')];
                      
        System.debug(Logginglevel.DEBUG,'License Key Id ' + LicenseKey.id);
       System.debug(Logginglevel.DEBUG,'License Key Name ' + LicenseKey.name);
                                      
      }

      public cs_LicenseKey__c getLicenseKey() {
            return LicenseKey;
      }
     
}

 

==========

The above code is works fine. And I want to add below code. They work fine independenlty. I have a problem merging them together.

=========

 

<apex:page controller="opportunityList2Con">
    <apex:pageBlock >
        <apex:pageBlockTable value="{!options}" var="o">
            <apex:column value="{!o.name}"/>
            <apex:column value="{!o.id}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

 

 

 

public class opportunityList2Con {
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([select name,id from cs_licensekeyoption__c where LicenseKey__c = :ApexPages.currentPage().getParameters().get('id')]));
               
            }
            return setCon;
        }
        set;
    }
    public List<cs_licensekeyoption__c> getOptions() {
         return (List<cs_licensekeyoption__c>) setCon.getRecords();
    }
}

 

 

ShivaShiva

Please ignore the previous code. Had some mistakes.

 

Thank you for your quick response. Please help me if you can. I am new to apex code.

 

 

 

<apex:smileytongue:age controller="MyController" tabStyle="cs_LicenseKey__c">
<apex:form >
    <apex:smileytongue:ageBlock title="Confirmation">
      <apex:smileytongue:ageBlockSection title="License Ky Information">
        <apex:smileysurprised:utputField value="{!LicenseKey.Id}"/>
        <apex:smileysurprised:utputField value="{!LicenseKey.name}"/>
      </apex:smileytongue:ageBlockSection>
    </apex:smileytongue:ageBlock>
  </apex:form>
</apex:smileytongue:age>

 


public class MyController {

        private final cs_LicenseKey__c LicenseKey;
      
        
        public MyController() {
            LicenseKey = [select id,name from cs_LicenseKey__c where id = :ApexPages.currentPage().getParameters().get('id')];
                      
        System.debug(Logginglevel.DEBUG,'License Key Id ' + LicenseKey.id);
       System.debug(Logginglevel.DEBUG,'License Key Name ' + LicenseKey.name);
                                      
      }

      public cs_LicenseKey__c getLicenseKey() {
            return LicenseKey;
      }
     
}

 

==========

The above code works fine. And I want to add below code. They work fine independenlty. I have a problem merging them together.

=========

 

<apex:smileytongue:age controller="opportunityList2Con">
    <apex:smileytongue:ageBlock >
        <apex:smileytongue:ageBlockTable value="{!options}" var="o">
            <apex:column value="{!o.name}"/>
            <apex:column value="{!o.id}"/>
        </apex:smileytongue:ageBlockTable>
    </apex:smileytongue:ageBlock>
</apex:smileytongue:age>

 

 

 

public class opportunityList2Con {
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator([select name,id from cs_licensekeyoption__c where LicenseKey__c = :ApexPages.currentPage().getParameters().get('id')]));
               
            }
            return setCon;
        }
        set;
    }
    public List<cs_licensekeyoption__c> getOptions() {
         return (List<cs_licensekeyoption__c>:smileywink: setCon.getRecords();
    }
}

 

gtuerkgtuerk

Now there's always a ton of ways to skin a cat.  How are you using the license key options?  Are you using this visualforce page in a list button?

 

If you need to use a StandardSetController then do it like this:

 

public class LicenseKeyOptionsController{

 

  cs_LicenseKey__c licenseKey;

 

  private final ApexPages.StandardSetController controller;

 

  public LicenseKeyOptionsController(ApexPages.StandardSetController controller){

     this.controller = controller;

licenseKey = [select Id, Name from cs_LicenseKey__c where id = :ApexPages.currentPage().getParameters().get('id')];

 

 

}

public cs_License_Key__c getLicenseKey{

return licenseKey;

}

 

public List<cs_licensekeyoption__c> getOptions() {
         return (List<cs_licensekeyoption__c>:smileywink: controller.getRecords();
    }

 

}

 

 

Otherwise, just use the cs_LicenseKey__c as your standardController for the page and only use the custom controller code as necessary.

ShivaShiva

Thanks for your quick response. Where will my other contoller code  go? Please can you help me to merge this code with your code? I am sorry to take your time. It will be helpful if you just give code for page too.

 

public class MyController {

        private final cs_LicenseKey__c LicenseKey;
      
        
        public MyController() {
            LicenseKey = [select id,name from cs_LicenseKey__c where id = :ApexPages.currentPage().getParameters().get('id')];
                      
        System.debug(Logginglevel.DEBUG,'License Key Id ' + LicenseKey.id);
       System.debug(Logginglevel.DEBUG,'License Key Name ' + LicenseKey.name);
                                      
      }

      public cs_LicenseKey__c getLicenseKey() {
            return LicenseKey;
      }
     
}