Using record-triggered flows, it's possible to trigger FormulaShare rule assessment directly and apply sharing as soon as records are created or updated.
A flow template is included in the package to make this as easy as possible - just a few clicks. This approach works for applying all standard rules and cross-object rules for certain changes.
With a rule or a set of rules created, open the Salesforce Setup menu, and navigate to Process Automation -> Flows. You should find the flow template Apply FormulaShare Rules in the list. Click the Flow Label to open the template:
We're first going to modify the template to operate on the right objects. Click the flow's Start element and change the object the flow operates on by clicking "Edit":
Select the object which should trigger sharing changes. For standard rules, this would be the shared object itself, but for cross-object rules this could be a related object which looks up to the shared object. For full details of which changes are considered by triggers and record-triggered flows for these rules, check the cross-object rules page.
There's no need to change anything else here, but notice that this flow runs whenever records are created or updated.
Next, set the shared object by opening the "Call FormulaShare" Apex Action connected to the flow's start element:
You might just have one object here if your object is only involved in standard rules.
In our case there are two objects shown - this is because we have standard rules set up on Donations, and changes to Donations can also trigger sharing changes on Programmes through application of cross-object rules. For situations like this, it's possible to apply both kinds of changes in real time by saving and activating two versions of the flow, one for each shared object.
Once you've selected the shared object, we're finished! Well, nearly. Click "Save As" to save this version of the flow, and make sure to activate it by clicking the "Activate" button:
Which rules will this apply?
FormulaShare is designed to work safely with existing automations, and in the context of record-triggered flows runs in "Transaction Safe" mode. More information is outlined in Transaction Safe or Full Assessment.
How sharing is applied
You might have noticed, there are some actions which happen in the flow after FormulaShare has been called:
These elements do the following:
- Insert any new sharing which should be applied for the records being assessed for the shared object
- Check whether any deletion is required (this check is needed to avoid an error if no deletion is needed)
- Delete sharing if necessary
Since FormulaShare is an approved managed package, it's given its own governor limits so won't contribute to most transaction limits which apply to your own code (number of SOQL queries, number of DML statements etc).
Note: You may have noticed that the apex action also has an option "Process Other Records". This allows you to apply sharing for all rules which share objects at any time. The approach here is the same - you'll want to insert and remove sharing using the same approach as the template flow. For more info, check the Assess Rules on Demand section.