+ Start a Discussion

Passing a variable into a child class constructor?

I'm using a custom lead converter - VF Page, controller class, and separate LeadConversion class (for the heavy lifting). This lead converter overwrites a handful of fields, adds a new opportunity (there are a handful of opp fields on the lead object), and attaches that person to the opportunity by adding a contact role, among other features. On the VF page, the user has the option to choose a record type for a given lead with opportunity data. In lieu of hard-coding actual opportunity record type values into a custom field on the lead, I'd like to dynamically generate these values using a describe method to populate a selectList. Sounds easy enough, no problems right.


The controller for the lead converter (which has a constructor) displays a list of leads to create, defined in the class leadResult, which in turn has its own constructor -- child class with a child class constructor. Currently, the describe method used to fetch the opportunity record types from the opportunity object is called for each lead in the leadResult class. For 20 leads on a page, 20 describe methods. The limit for describe calls in one transaction appears to be 10, which crashes my lead converter for anything over 10 lead records, causes tests to fail, etc.

Solution, and more problems:
The solution is to move my describe method outside of the leadResult class, so it isn't called for each lead on the page. However, I can't seem to move the describe to the top of the class and still access the variable holding the list of opportunity record types. I get the error that the variable does not exist. I've tried placing the describe call at the top of the class and in the controller constructor, both to no avail. I've also tried making the List variable public without success. I've even tried moving the describe method to a separate class, but since I'm referencing it from the leadResult still, it's calling a describe method each time. I don't quite understand why I can't see/use my variable from the top of my class, or when it's placed in the constructor, any ideas?

Here's my significantly truncated code:

public class ONEN_CTRL_LeadConverter {

 public ONEN_CTRL_LeadConverter() {
  //individual account id
  IndividualAccountId = ONEN_DefaultAccount.getIndividualAccountId();
  //individual account name
  IndividualAccountName = ONEN_Constants.INDIVIDUAL_ACCOUNT_NAME;
  //set the org name not provided default label
  NotProvidedLabel = ONEN_Constants.NOT_PROVIDED_LABEL;
    // this class represents one row in the leads list
    public class leadResult {
  // constructor
     public leadResult(ONEN_LeadConversion.LeadMatchup matchup) { 
      //grab the Opp RecordTypes and throw them into a SelectOptions list
   Schema.DescribeSObjectResult OppRT = Opportunity.sObjectType.getDescribe();
   List<Schema.RecordTypeInfo> RT = OppRT.getRecordTypeInfos();
      RToptions.add(new SelectOption('','--Select Record Type--'));
         for ( Schema.RecordTypeInfo thisRT : RT ) {
          RToptions.add(new SelectOption(thisRT.getName(),thisRT.getName()));

Seems to me like the child class constructor is being called before the controller constructor and therefore can't access any variables set there (or anywhere else in the class for that matter).  Any ideas?

Thanks in advance.
Bump, any ideas about where to place this describe method?

Here is an example of working code. This does a global describe, but you can use the same technique for a more limited describe:


    public static Map<String, Schema.SObjectType> globalDescribe {
        get {
            if (globalDescribe == Null) {
                globalDescribe = Schema.getGlobalDescribe();
            return globalDescribe;