Spark Running Totals with Construct Entity Field
Spark can keep a running total in a CRM entity field, such as "Lifetime Giving" or "Total Volunteer Hours", by adding to it every time an event happens. This is sometimes called a calculated field. It uses the Construct Entity Field outcome with a short piece of Smarty code.
Before you start
- Create the entity field that will hold the total, for example "Lifetime Giving" on a donor entity. See CRM Configuration: Entities, Fields and Relationship Types.
- Decide which event should update it, for example New Contribution, or Task Complete for a counter of completed tasks.
- Decide how the total changes each time: add the gift amount, or add 1.
Set it up
- In Spark, add a rule with the event you chose and set its conditions.
- Add a Construct Entity Field outcome and choose the CRM record to update (for example the donor).
- Choose the entity and the field that holds the total.
- Enter the Smarty code that builds the new value (see below).
- Click Save & Close, then test it.
The calculation
The code adds the new value to the field's current value. Always check whether the field already has a value: the first time, there is nothing to add to, so the code should just use the new value.
{if isset($current_total)}
{$current_total + $gift_amount}
{else}
{$gift_amount}
{/if}
Note: $current_total and $gift_amount are placeholders. Use the variables the outcome lists for your entity field and event. For a counter, replace the gift amount with 1.
Test it
- Test with a record whose field is empty, to check the first value is set.
- Test again with the same record, to check the value is added to.
Keep totals accurate
A running total only changes when its event fires. It won't change when an older gift is edited or deleted, when gifts are imported, or if an event is missed. Keep a report that calculates the same total from your data, so you can:
- fill in the starting totals for existing records when you set this up, and
- check and correct totals from time to time.
.png)
.png)