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 

It may be very simple, but I need help (new to apex code.)

Hi Guys,

 

I have two controllers and they are tied to idependent objects. Object names are cs_licenseKey__c and cs_Licensekeyoption__c. I am able to run these controllers one at a time on my custom visualforce page. But I am having difficulty to merge these two controller into one and display the data on the same page. I have pasted the code below. If you can help me to merge these two that will be great. Thanks in advance.

 

Remember second controller will return more than one record. But the first controller has only one record.

 

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

 

Controller one:

=========

 

<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;
      }
     
}

 

Controller Two

===========

 

<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();
    }
}

BritishBoyinDCBritishBoyinDC

If cs_licensekeyoption__c is a child of cs_LicenseKey__c, you should be able to include the [select name,id from cs_licensekeyoption__c] SOQL in the first controller,so you'd have something like this:

 

[select id,name,(select name,id from cs_licensekeyoption__c) from cs_LicenseKey__c where id = :ApexPages.currentPage().getParameters().get('id')];]

 

BUT - the tricky bit is you need to know the Parent-Child Relationship name, which you can set in the lookup field from child to parent, and then use the Force IDE schema explorer to work out what the correct syntax is for that parent child relationship SOQL.

 

Once you have that SOQL working,  you can access the child data in a table, in the same way as the example, where cs_LicenseKey__c is the parent, and 'contacts' should be replaced by the child relationship name for cs_licensekeyoption__c

 

 

<apex:pageBlockTable> component to list the contacts associated with an account that is currently in context:<apex:page standardController="Account"> <apex:pageBlock title="Hello {!$User.FirstName}!"> You are viewing the {!account.name} account. </apex:pageBlock> <apex:pageBlock title="Contacts"> <apex:pageBlockTable value="{!account.Contacts}" var="contact"> <apex:column value="{!contact.Name}"/> <apex:column value="{!contact.MailingCity}"/> <apex:column value="{!contact.Phone}"/> </apex:pageBlockTable> </apex:pageBlock></apex:page>

 

 

 

 

ShivaShiva

Thanks for your respose. But I am getting "Error: Compile Error: Illegal assignment from LIST:SOBJECT:cs_LicenseKeyOption__c to SOBJECT:cs_LicenseKey__c at line 14 column 9"

 

Line 14 is red in color.

 

But the query works fine on SOQL.

 

 

public class MyController {
    public MyController(ApexPages.StandardController controller) {

    }

        private final cs_LicenseKey__c LicenseKey;
        
       public MyController() {
       
        LicenseKey = [select id,name,op.licensekey__r.CPUCount__c,op.licensekey__r.name,op.licensekey__r.id from   cs_licensekeyoption__c op
                        where licensekey__c = :ApexPages.currentPage().getParameters().get('id')];

                               
      }

      public cs_LicenseKey__c getLicenseKey() {
            return LicenseKey;
      }
     
}

ShivaShiva

One step ahead. Now I am getting the following error at the same line 14.

 

 

 

System.QueryException: List has more than 1 row for assignment to SObject

 

 

public class MyController {
    public MyController(ApexPages.StandardController controller) {

    }

        private final cs_LicenseKeyOption__c LicenseKey;
        
       public MyController() {
       
        LicenseKey = [select id,name,op.licensekey__r.CPUCount__c,op.licensekey__r.name,op.licensekey__r.id from   cs_licensekeyoption__c op
                        where licensekey__c = :ApexPages.currentPage().getParameters().get('id')];

                               
      }

      public cs_LicenseKeyOption__c getLicenseKey() {
            return LicenseKey;
      }
     
}

BritishBoyinDCBritishBoyinDC

You need to declare Licensekey as a list, not an object:

 

 List<cs_LicenseKey__c> LicenseKey;

BritishBoyinDCBritishBoyinDC
Thinking about this more, if that SOQL works, you don't need a custom controller - the standard controller for cs_LicenseKey__c will work - when you pass in an Id for cs_LicenseKey__c, you can then use an Apex datatable to reference the child records in a table, in the same way the Account-Contact example works in the VisualForce docs...
ShivaShiva

That was wonderful thinking and it worked for me. Little further what if parent has more than one child? How for can we go like this?

 

Thank you very much for your help.

 

Thanks

Shiva