-
Notifications
You must be signed in to change notification settings - Fork 28
Getting Started with 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:
- User
- Public Group
- Profile
- 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
.
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...
}
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
}
});
}
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);
});
}
})
In Flow Builder, Global
feature switches can easily be accessed using the Get Records
element. See the screenshot below for a sample configuration.
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.
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