Request via Flow or Apex

Assessment on Demand

FormulaShare provides the means to assess sharing changes for all rules acting a supplied record or set of records.

This mechanism can be called from either Apex or Flow, so can be integrated with your own customisations.

Should I use this approach?

In most implementations this isn't required. The use of the standard Record Triggered Flow template or Apex Trigger code ensures that rules are assessed in a Transaction Safe mode to reduce likelihood of affecting other automation on the object. Together with the scheduled batch job (or targeted calculation jobs) this provides a comprehensive means of keeping sharing up to date for all records.

Requesting assessment directly might be helpful for specific implementation scenarios such as:

  • Requesting recalculation for a record on demand via a screen flow or an LWC component
  • Applying sharing changes in response to trigger operations in an asynchronous context, such as a Queueable Apex class
  • Triggering recalculation of sharing via an API

 

Requesting via Flow

Requesting via flow can be done using the Assess FormulaShare Sharing Apex Action. This is the same action used in the "Apply FormulasShare Rules" Record Triggered Flow template.

To use the action, add this to any kind of flow by creating a new Apex Action element and select "Assess FormulaShare Sharing":

Screenshot of Apex Action component

 

Note the following considerations around using this component:

  • The values available to select in the field "Assess Sharing For" are flow variables which are set up to hold one or multiple records
  • To assess output of elements earlier in the flow (e.g. records retrieved via a Get element), it's necessary to first assign the outputs to a flow variable using an Assignment element
  • The action will return two output variables "Shares to Create" and "Shares to Delete":
    • Records in the Shares to Create variable can be created using a Create element without inspection
    • Records in the Shares to Delete variable should be checked first using a Decision element - if the variable is null, no further action in the flow is needed. If not, a Delete element should be used to remove these records. This decision is necessary because the flow engine will throw an exception while attempting to delete an empty or null variable
    • For reference of how set up this logic, check the "Apply FormulasShare Rules" flow template

 

Requesting via Apex

A global class is provided in the package to assess FormulaShare rules for one or multiple records. The examples below illustrate how this can be used.

Assessing a single record:

    Donation__c donation = [SELECT Id FROM Donation__c WHERE Name = 'ThatBigDonation' LIMIT 1][0];
    sdfs.FormulaShareHelper helper = new sdfs.FormulaShareHelper(donation);
    insert helper.getSharesToInsert();
    delete helper.getSharesToDelete();

Assessing for a list of records:

    List<Donation__c> donations = [SELECT Id FROM Donation__c WHERE CreatedDate >= LAST_N_WEEKS:2];
    sdfs.FormulaShareHelper helper = new sdfs.FormulaShareHelper(donations);
    insert helper.getSharesToInsert();
    delete helper.getSharesToDelete();

Note that all records must include a value in the Id field, and when assessing for a list, all records must be of the same SObject type. If this isn't the case, an exception will be thrown.

As with code for triggers, the insert and delete statements should be run in a without sharing context unless you're sure that any users invoking these have full access to the shared objects.

 

Recursive assessment needed?

By default, FormulaShare will only assess sharing on a record once in a given transaction - if the same record is submitted to FormulaShare again in the same transaction sharing assessment will be skipped.

When requesting assessment via flow or apex more than once for the same records in the same transaction, or when FormulaShare is called from apex triggers or record triggered flows and these trigger operations could also result in requests via flow or apex for the same records, it may be necessary to to allow FormulaShare to run more than once for this object. This can be done by using the "Allow Recursive Assessment" option in object settings.

Screenshot of allow recursive assessment option