Custom Settings in Apex

You are not limited to using the native user interface for managing data in custom settings, as demonstrated in Chapter 2. Custom settings can also be created, updated, and deleted using standard DML methods. This means you can build your own user interfaces for managing them, or use them to store frequently accessed, simple configuration values needed by your programs. Force.com provides increased performance for custom settings access versus ordinary database access, and custom settings are exempt from the governor limits placed on database access. For example, you might use a custom setting named Debug as a global switch to enable verbose logging within your Apex code.

To get started with custom settings in Apex, run the code in Listing 5.36. It inserts a custom setting record, setting its name and its field value. It assumes you already have defined a List type custom setting object named ConfigSetting containing a single Checkbox field named Debug.

Listing 5.36 Creating a Custom Setting Record


insert new ConfigSetting__c(Name = 'Default', Debug__c = false);


Now that your custom setting has a value, try retrieving it. Run the code in Listing 5.37 in the Force.com IDE’s Execute Anonymous view.

Listing 5.37 Retrieving a Custom Setting Value


ConfigSetting__c cfg = ConfigSetting__c.getValues('Default'),
System.debug(cfg.Debug__c);


The first line retrieves the named record, Default, which you created in Listing 5.36. The second line prints the value of the custom field to the debug log. You can also retrieve a Map of all fields and values using the getAll method.

To update a custom setting value, retrieve it by name, and then update it as you would a database record. Listing 5.38 provides an example.

Listing 5.38 Updating a Custom Setting Record


ConfigSetting__c cfg = ConfigSetting__c.getValues('Default'),
cfg.Debug__c = true;
update cfg;


You can also delete custom setting records using the delete DML method, as shown in Listing 5.39.

Listing 5.39 Deleting a Custom Setting Record


ConfigSetting__c cfg = ConfigSetting__c.getValues('Default'),
delete cfg;


Hierarchy type custom settings allow a user or profile to be related to them. If no user or profile is specified, they become organization-wide defaults. The code in Listing 5.40 assumes you have created a Hierarchy type custom setting named HierarchySetting with a single text field named Field. It creates a new record and relates it to the current user by setting the system field SetupOwnerId to the current user’s unique identifier. This same field also accepts a profile unique identifier to make the custom setting apply to a profile instead of a user. And if SetupOwnerId is set to null, it becomes an organization-wide default.

Listing 5.40 Creating a Hierarchy Type Custom Setting Record


insert new HierarchySetting__c(
  SetupOwnerId = UserInfo.getUserId(),
  Field__c = 'My user preference value'),


To retrieve a Hierarchy type custom setting value, use the getInstance method of the custom setting object. By default, it returns the “lowest” level of setting value, meaning the value most specific to the current user. If a user-level setting is available, it is returned. Otherwise, the return value is the setting associated with the user’s profile. If no user or profile-level settings are present, the organization-wide default is returned. This behavior can be overridden by passing a user or profile unique identifier as an argument to the getInstance method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.64.78