You need to sign in to do that
Don't have an account?
Shiva
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.
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
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();
}
}
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.
<apexage controller="MyController" tabStyle="cs_LicenseKey__c">
<apex:form >
<apexageBlock title="Confirmation">
<apexageBlockSection title="License Ky Information">
<apexutputField value="{!LicenseKey.Id}"/>
<apexutputField value="{!LicenseKey.name}"/>
</apexageBlockSection>
</apexageBlock>
</apex:form>
</apexage>
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.
=========
<apexage controller="opportunityList2Con">
<apexageBlock >
<apexageBlockTable value="{!options}" var="o">
<apex:column value="{!o.name}"/>
<apex:column value="{!o.id}"/>
</apexageBlockTable>
</apexageBlock>
</apexage>
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();
}
}
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> controller.getRecords();
}
}
Otherwise, just use the cs_LicenseKey__c as your standardController for the page and only use the custom controller code as necessary.
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;
}
}