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
jmischojmischo 

Error when passing a custom setting object into a constructor

I'm working on a [soon to be open-sourced] application configuration object that allows an application to programmatically check for a specific custom setting and load a default value from a file if it hasn't been set.  To that end, I've got a constructor for this class that requires a custom settings object and a string to pass to getValues() on that object.

 

Unfortunately, when I attempt to call getInstance() on the object (which I've tried using both SObject and Object s the primitive for), the Force.com IDE gives me a save error.  I'd really appreciate any insight into what the correct primitive type would be for this object (hopefully there's an undocumented primitive that has getInstance(string) defined on it?).

 

The errors I get are below - I get a similar error if I use getValues() instead of getInstance():

Save error: Method does not exist or incorrect signature: [SObject].getInstance(string)

or

Save error: Method does not exist or incorrect signature: [Object].getInstance(string)

 

The constructor in question is pretty simple (see below), with the error appearing on line 2 of 2 in the method body.  Both lines are assigning values to private instance variables for use by other methods in the class.

The constructor:

 

<code> 

    public ApplicationConfigurator( String defaultConfigurationResourceName,

 Object systemConfigurationObject,

 String systemConfigurationInstanceName)

    {

 defaultConfigurationFileResource = defaultConfigurationResourceName;

 systemConfigInstance = systemConfigurationObject.getValues(systemConfigurationInstanceName);

    }

</code>

jmischojmischo

Anyone have any ideas at all on this?

Kevin BromerKevin Bromer

getInstance and getValues are instance methods of the child custom setting object itself, not the sobject/object parent.  An sobject/object doesn't know what those methods are.  If you unbox the object into an instance of your settings type, than you already have an instance of the object, so you don't need get values, it IS a value.  Otherwise, you'd have a collection (a map) that is the result of a getAll() method call, which returns a <string, custom_setting__c> map. With that, you can do:

mymap.get(defaultConfigurationResourceName);, which would return an instance of your custom object that has the name of 'defaultConfigurationResourceName'.  If you're just passing the object though and your custom setting was called my_custom_setting__c, you could do something like:

 

    
public ApplicationConfigurator( String defaultConfigurationResourceName,
Object systemConfigurationObject,
String systemConfigurationInstanceName)
{
 my_custom_setting__c systemConfigInstance =  (my_custom_setting__c)systemConfigurationObject;
}

 HTH