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
Deb Flores 1Deb Flores 1 

Test Class Compile Error - Variable does not exist: Opportunity

Hi everyone!  I'm a fairly new Apex coder (I'm an old Assembler programmer from back in the day :-)  but still learning Java and Apex), and I can't figure out why this test class won't compile.  I have a feeling it's something very obvious that I'm overlooking, because 1) it's a very basic test class, and 2) I'm using virtually the same code in another test class and it works swimmingly.  If someone would be kind enough to take a look and point out the error, I'd really appreciate it!

Here's the test class:
@isTest(SeeAllData=true)
private class UtilGetPickListValuesTest {
   static testmethod void testPickListValues() {

	Test.startTest();
	list<SelectOption> plValues = new list<SelectOption>();	
	plValues = UtilGetPickListValues.getPicklistValues(Opportunity, 'StageName');
	System.debug('plValues = ' + plValues); 
	Test.stopTest();
	
	System.assert(plValues.size() > 0);
   }
}
and here's the utility class it will cover:
global without sharing class UtilGetPickListValues {
	
// Get a list of picklist values from an existing object field.
   global static list<SelectOption> getPicklistValues(SObject obj, String fld)
   {
      list<SelectOption> options = new list<SelectOption>();
      // Get the object type of the SObject.
      Schema.sObjectType objType = obj.getSObjectType(); 
      // Describe the SObject using its object type.
      Schema.DescribeSObjectResult objDescribe = objType.getDescribe();       
      // Get a map of fields for the SObject
      map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 
      // Get the list of picklist values for this field.
      list<Schema.PicklistEntry> values =
         fieldMap.get(fld).getDescribe().getPickListValues();
      // Add these values to the selectoption list.
      for (Schema.PicklistEntry a : values)
      { 
         options.add(new SelectOption(a.getLabel(), a.getValue())); 
      }
      return options;
   }

}

And while I'm writing, I'd like to say thanks to all of you who take time to offer your help and advice on the developer community!  It is an invaluable resource to me, and I'm sure to many others as well!

Deb



 
Best Answer chosen by Deb Flores 1
John PipkinJohn Pipkin
Deb, 

Change your method input "obj" to a String data type. Then when you are setting your Schema.SObjectType, use "Schema.getGlobalDescribe().get(obj);"

That should get rid of that error.
 
global without sharing class UtilGetPickListValues {
	
// Get a list of picklist values from an existing object field.
   global static list<SelectOption> getPicklistValues(String obj, String fld)
   {
      list<SelectOption> options = new list<SelectOption>();
      // Get the object type of the SObject.
      Schema.sObjectType objType = Schema.getGlobalDescribe().get(obj);
      // Describe the SObject using its object type.
      Schema.DescribeSObjectResult objDescribe = objType.getDescribe();       
      // Get a map of fields for the SObject
      map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 
      // Get the list of picklist values for this field.
      list<Schema.PicklistEntry> values =
         fieldMap.get(fld).getDescribe().getPickListValues();
      // Add these values to the selectoption list.
      for (Schema.PicklistEntry a : values)
      { 
         options.add(new SelectOption(a.getLabel(), a.getValue())); 
      }
      return options;
   }

}

 

All Answers

John PipkinJohn Pipkin
Deb, 

Change your method input "obj" to a String data type. Then when you are setting your Schema.SObjectType, use "Schema.getGlobalDescribe().get(obj);"

That should get rid of that error.
 
global without sharing class UtilGetPickListValues {
	
// Get a list of picklist values from an existing object field.
   global static list<SelectOption> getPicklistValues(String obj, String fld)
   {
      list<SelectOption> options = new list<SelectOption>();
      // Get the object type of the SObject.
      Schema.sObjectType objType = Schema.getGlobalDescribe().get(obj);
      // Describe the SObject using its object type.
      Schema.DescribeSObjectResult objDescribe = objType.getDescribe();       
      // Get a map of fields for the SObject
      map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); 
      // Get the list of picklist values for this field.
      list<Schema.PicklistEntry> values =
         fieldMap.get(fld).getDescribe().getPickListValues();
      // Add these values to the selectoption list.
      for (Schema.PicklistEntry a : values)
      { 
         options.add(new SelectOption(a.getLabel(), a.getValue())); 
      }
      return options;
   }

}

 
This was selected as the best answer
Deb Flores 1Deb Flores 1
Many thanks for your help, John!  It makes perfect sense, and fixed the problem!

Deb
John PipkinJohn Pipkin
No problem!