Skip to content

Getting Started with Feature Switches

Johannes Fischer edited this page Dec 30, 2022 · 3 revisions

Feature Switches

Feature switches are an integral part of modern org development. For example, they allow features to be deployed but hidden from the users for an extended period of time. They also enable A/B testing, or provide the ability to turn functionality, i.e. a system integration, off during an outage. All of this can be done using Salesforce configuration interface instead of requiring a full deployment. The Feature Switch implementation of rflib is based on Custom Metadata Types, but includes an implementation that enables a hierarchical configuration users know and love about Custom Settings.

To use Features Switches, simply add a new record to the Custom Metadata Type called "Feature Switch" and check the switch using the Apex utility class rflib_FeatureSwitch.

Feature switches are evaluated in the following order where the first value found in the hierarchy will be returned:

  1. User
  2. Public Group
  3. Profile
  4. Global

Public Groups require a direct Group Membership. If multiple groups are assigned, they are evaluated in alphabetical order based on the DeveloperName of the Public Group.

The default value for a feature switch without any matching configuration is false.

Feature Switch in Apex

Below is an example on how the feature switch is evaluated in the Trigger Manager class.

private static void dispatch(rflib_TriggerManager.Args args) {
    List<TriggerHandlerInfo> handlers = getHandlers(args);

    if (rflib_FeatureSwitch.isTurnedOff('All_Triggers')) {
        LOGGER.warn('All Trigger Feature switch turned off, exiting trigger execution.');
        return;
    }

    // more code...
}

Feature Switch in LWC

You can also use features switches on the client side. For LWC, use the following syntax:

import { isFeatureSwitchTurnedOn } from 'c/rflibFeatureSwitches';

handleSomeEvent(event) {
    isFeatureSwitchTurnedOn('mySwitchName')
        .then(isTurnedOn => {
            if (isTurnedOn) {
                // do something
            }
        });
}

Feature Switch in Aura

In Aura components, add the feature switch component in the .cmp file.

<c:rflibFeatureSwitches aura:id="featureSwitches" />

In the controller or helper, you can then validate a feature switch with the following code snippet:

({
	doInit: function(component, event, helper) {
		var featureSwitches = component.find('featureSwitches');
		featureSwitches.isFeatureSwitchTurnedOn('All_Triggers')
			.then(function (isTurnedOn) {
				logger.info('All_Triggers turned on? ' + isTurnedOn);
			});
	}
})

Feature Switch in Flows

In Flow Builder, Global feature switches can easily be accessed using the Get Records element. See the screenshot below for a sample configuration.

alt text

If you would like to retrieve a full hierarchical feature switch value, that can be overwritten on a profile of user level, use the Apex Action displayed below.

alt text

Feature Switch in Validation Rules

Validation Rules can also be configured to consider feature switches. However, there is one limitation and that is that the Feature Switch must be mentioned by API name. This will make it more difficult to consider the hierarchy, therefore it is recommended to only use feature switches with a global scope in validation rules.

Below is an example of a formula using a feature switch, which is taken from the demo project:

$CustomMetadata.rflib_Feature_Switch__mdt.Data_Load_In_Progress.Turned_On__c = FALSE &&
TODAY() - Date_Listed__c > 15
Clone this wiki locally