Feature Flag Framework in Salesforce Using LaunchDarkly
A Salesforce feature flag system using custom permissions and LaunchDarkly enables controlled rollouts, rapid reversals, and seamless feature management.
Join the DZone community and get the full member experience.
Join For FreeReleasing new features in a Salesforce environment can sometimes feel like walking a tightrope; one misstep can take down mission-critical processes. That’s why feature flagging has emerged as a powerful strategy. Instead of deploying features to everyone all at once, you introduce them incrementally, fine-tuning your approach along the way.
In this article, I’ll share how I built a feature flag framework using custom permissions, permission sets, and an integration with LaunchDarkly. This setup has helped my team safely roll out new Salesforce functionality while maintaining complete control over who sees what and when.
The Pain Point
When multiple teams collaborate in a single Salesforce org, rolling out new features can quickly spiral into chaos, especially if you’re dealing with multiple deployment windows or a large user base. A typical scenario might look like this:
- Multiple new functionalities are ready to go live around the same time.
- You want certain features to reach a small percentage of users for an initial pilot.
- Another set of functionalities needs to be turned on for just a subset of roles or profiles.
Relying on standard Salesforce profiles and permission sets alone won’t always cut it for a graceful, phased rollout. You need something more dynamic.
Why Feature Flags?
Feature flags allow you to turn functionality on or off without the hassle of multiple code deployments. This approach is particularly useful when you:
- Want to test new features with small user groups (e.g., 10% of the org).
- Need to disable a feature in seconds if it causes issues.
- Don’t want to deploy another package or set of changes just to tweak permissions.
The Core Architecture
1. Custom Permissions
- Each new feature is paired with a custom permission in Salesforce (e.g., FEAT_NewFeatureXYZ).
- Custom permissions are easy to reference in Apex, Lightning Web Components, and Flow, making them a great backbone for feature toggles.
2. Permission Sets
- Create a dedicated permission set for each feature and attach the relevant custom permission.
- Assigning this permission set to a user effectively flags the feature on for that user, and removing it flags the feature off.
3. LaunchDarkly Integration + Custom Object
- Whenever a new flag is created in LaunchDarkly, the integration automatically creates (or updates) a custom object record in Salesforce that includes:
- The LaunchDarkly flag details (e.g., name, environment).
- The rollout percentage (e.g., 50%, 100%) or status (enabled/disabled).
- A batch Apex job periodically runs after record, create, or update and handles permission set assignments or removals.
How It All Works
1. New Flag Creation in LaunchDarkly
- Let’s say you create a flag called FEAT_NewDashboard in LaunchDarkly, configured for a 50% rollout.
- The integration fires, automatically creating or updating a custom object record in Salesforce that says, in essence:
"Flag: FEAT_NewDashboard, Rollout: 50%.”
2. Salesforce Batch Assignment
- A batch job reads this record and assigns the permission set with the flag name to 50% of the relevant user base.
- If the record states a 100% rollout, the batch job assigns it to all users.
- If it’s disabled or 0%, the batch job removes the permission set from everyone.
3. User Checks
- In Apex, Flow, Lightning Web Components code checks if a user has the custom permission (via the assigned permission set).
- If FEAT_NewDashboard is assigned, the user sees the new dashboard. Otherwise, it remains hidden.
4. Ongoing Updates
- If you later decide to increase the rollout to 75% in LaunchDarkly, the Salesforce custom object will be updated accordingly.
- The batch job runs after record update, it sees the updated 75% rollout and assigns the permission set to an additional 25% of users, bringing the total to 75%.
- If issues occur, toggling the LaunchDarkly flag to “off” will change the salesforce record to disabled. The next batch run removes the permission set from all users, effectively shutting off the feature.
5. Gradual Rollouts in Action
- Want to start small? Set the rollout to 5% in LaunchDarkly. The batch job assigns the permission set to 5% of users.
- If everything goes well, crank it up to 50%, 75% and eventually 100%. Should something go wrong, you can switch the LaunchDarkly flag to “disabled” and the subsequent batch job revokes the permission set, no code redeployment or manual user-by-user removal needed.
Why This Approach Rocks
- Automated control: Once you set up the custom object and integration, developers can manage rollout percentages purely in LaunchDarkly.
- Real-time rollouts: Integration jobs run at your chosen interval, so changes in LaunchDarkly reflect in Salesforce relatively quickly.
- Fewer deployment headaches: No need to push new code when toggling a feature on or off everything happens through data and permission sets.
- Safe testing: Roll out to a subset of users, gather feedback, and gradually expand. If something breaks, turn it off instantly via the LaunchDarkly flag.
Final Thoughts
Feature flagging in Salesforce doesn’t have to be complicated. You can transform how you roll out changes by harnessing custom permissions, permission sets, and a well-structured integration with LaunchDarkly (or any similar tool). This framework offers flexibility and quick reaction times, a big win for teams that need to innovate without jeopardizing stability.
Opinions expressed by DZone contributors are their own.
Comments