Newer Version Available
Custom Settings Methods
Usage
Custom settings methods are all instance methods, that is, they are called by and operate on a specific instance of a custom setting. There are two types of custom settings: hierarchy and list. There are two types of methods: methods that work with list custom settings, and methods that work with hierarchy custom settings.
For more information on creating custom settings in the Salesforce user interface, see “Create Custom Settings” in the Salesforce online help.
Custom Setting Examples
The following example uses a list custom setting called Games. The Games setting has a field called GameType. This example determines if the value of the first data set is equal to the string PC.
Hierarchy Custom Setting Examples
In the following example, the hierarchy custom setting GamesSupport has a field called Corporate_number. The code returns the value for the profile specified with pid.
The following example shows how to use hierarchy custom settings methods. For getInstance, the example shows how field values that aren't set for a specific user or profile are returned from fields defined at the next lowest level in the hierarchy. The example also shows how to use getOrgDefaults.
- Organization settings
- OverrideMe: Hello
- DontOverrideMe: World
- Profile settings
- OverrideMe: Goodbye
- DontOverrideMe is not set.
- User settings
- OverrideMe: Fluffy
- DontOverrideMe is not set.
Country and State Code Custom Settings Example
This example illustrates using two custom setting objects for storing related information, and a Visualforce page to display the data in a set of related picklists.
In the following example, country and state codes are stored in two different custom settings: Foundation_Countries and Foundation_States.
- Country Code
- State Code
- State Name
List Custom Setting Methods
The following are instance methods for list custom settings.
getAll()
Signature
public Map<String, CustomSetting__c> getAll()
Return Value
Type: Map<String, CustomSetting__c>
Usage
If no data set is defined, this method returns an empty map.
getInstance(dataSetName)
Signature
public CustomSetting__c getInstance(String dataSetName)
Parameters
- dataSetName
- Type: String
Return Value
Type: CustomSetting__c
getValues(dataSetName)
Signature
public CustomSetting__c getValues(String dataSetName)
Parameters
- dataSetName
- Type: String
Return Value
Type: CustomSetting__c
Usage
If no data is defined for the specified data set, this method returns null.
Hierarchy Custom Setting Methods
The following are instance methods for hierarchy custom settings.
getInstance()
Signature
public CustomSetting__c getInstance()
Return Value
Type: CustomSetting__c
Usage
This method is equivalent to a method call to getInstance(User_Id) for the current user.
Example
- Custom setting data set defined for the user: If you have a custom setting data set defined for the user “Uriel Jones,” for the profile “System Administrator,” and for the organization as a whole, and the user running the code is Uriel Jones, this method returns the custom setting record defined for Uriel Jones.
- Merged fields: If you have a custom setting data set with fields A and B for the user “Uriel Jones” and for the profile “System Administrator,” and field A is defined for Uriel Jones, field B is null but is defined for the System Adminitrator profile, this method returns the custom setting record for Uriel Jones with field A for Uriel Jones and field B from the System Administrator profile.
- No custom setting data set record defined for the user: If the current user is “Barbara Mahonie,” who also shares the “System Administrator” profile, but no data is defined for Barbara as a user, this method returns a new custom setting record with the ID set to null and with fields merged based on the fields defined in the lowest level in the hierarchy.
getInstance(userId)
Signature
public CustomSetting__c getInstance(ID userId)
Parameters
- userId
- Type: ID
Return Value
Type: CustomSetting__c
Usage
If no custom setting data is defined for the user, this method returns a new custom setting object. The new custom setting object contains an ID set to null and merged fields from higher in the hierarchy. You can add this new custom setting record for the user by using insert or upsert. If no custom setting data is defined in the hierarchy, the returned custom setting has empty fields, except for the SetupOwnerId field which contains the user ID.
getInstance(profileId)
Signature
public CustomSetting__c getInstance(ID profileId)
Parameters
- profileId
- Type: ID
Return Value
Type: CustomSetting__c
Usage
If no custom setting data is defined for the profile, this method returns a new custom setting record. The new custom setting object contains an ID set to null and with merged fields from your organization's default values. You can add this new custom setting for the profile by using insert or upsert. If no custom setting data is defined in the hierarchy, the returned custom setting has empty fields, except for the SetupOwnerId field which contains the profile ID.
getOrgDefaults()
Signature
public CustomSetting__c getOrgDefaults()
Return Value
Type: CustomSetting__c
Usage
If no custom setting data is defined for the organization, this method returns an empty custom setting object.
getValues(userId)
Signature
public CustomSetting__c getValues(ID userId)
Parameters
- userId
- Type: ID
Return Value
Type: CustomSetting__c
Usage
Use this if you only want the subset of custom setting data that has been defined at the user level. For example, suppose you have a custom setting field that has been assigned a value of "alpha" at the organizational level, but has no value assigned at the user or profile level. Using getValues(UserId) returns null for this custom setting field.
getValues(profileId)
Signature
public CustomSetting__c getValues(ID profileId)
Parameters
- profileId
- Type: ID
Return Value
Type: CustomSetting__c
Usage
Use this if you only want the subset of custom setting data that has been defined at the profile level. For example, suppose you have a custom setting field that has been assigned a value of "alpha" at the organizational level, but has no value assigned at the user or profile level. Using getValues(ProfileId) returns null for this custom setting field.