---
title: "How to set-up the 'Calculation Rules' package?"
canonical: "https://help.fieldbuddy.com/space/FBDOCS/6103105540/How%20to%20set-up%20the%20'Calculation%20Rules'%20package%3F"
format: markdown
---
The Calculation Rule functionality in FieldBuddy automates actions on **Work Order Line Item** and **Timesheet Lineitem** records based on predefined conditions. This system helps streamline processes such as pricing adjustments, discounts, or other calculations that depend on specific criteria.

> ⚠️ Calculation Rules is a [unlocked package](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_unlocked_pkg_whats_a_package.htm), an extension of the main FieldBuddy package. Modifications to the components in the package are at your own risk and updates to the 'calculation rules'-package can **no longer** be installed.

> ℹ️ This package is not part of FieldBuddy standard support.

## Installation of the package

1. The installation URL for the latest package version is:  
<u>[https://login.salesforce.com/packaging/installPackage.apexp?p0=04tQx000000Uu6PIAS](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tQx000000Uu6PIAS)</u>
2. You can also install it using the CLI command:  
sf package install --package 04tQx000000Uu6PIAS

## Key Objects

1. **Service Agreement**: A record that defines the terms of service, potentially covering specific Installed Products, Locations, or Accounts.
2. **Service Agreement Coverage**: Specifies the scope of coverage under a Service Agreement, detailing which Installed Products, Locations, or Accounts are included.
3. **Calculation Rule**: Defines the rules and actions to be applied to records. These rules are associated with a Service Agreement and include **Rule Conditions** and **Rule Actions**.
4. **Rule Condition**: Sets the criteria that records must meet for the rule to apply. Conditions are based on fields and operators (e.g., equals, not equal).
5. **Rule Action**: Outlines the updates to be made to a record when conditions are met, such as modifying a field value using arithmetic operations.
6. **Matched Calculation Rule**: Logs which Calculation Rules were applied to specific records, providing traceability and record-keeping.

## Creating a Calculation Rule

1. **Access Calculation Rules**: Navigate to the related list of Calculation Rules on the Service Agreement record.
2. **Create a New Rule**: Click "New" and enter the rule’s details, including its name and associated Service Agreement.
3. **Define Conditions**:

- **Condition Types**: Supports two types of conditions: **Formula** and **Value**.
- **Formula**: Evaluates a dynamic formula against the target object, with support for complex formulas referencing fields. (*Current bug: Formulas that refer to fields from parent objects are not yet supported.*)
- **Value**: Compares a field against a specified value using operators like equals or not equals.
- Select the Object (e.g., Work Order Line Item, Timesheet Lineitem).
- Set the Field, Operator (e.g., equals, not equal), and Value, or enter the Formula.
- All conditions must be met (logical AND) for the rule to apply, and conditions are evaluated for records of the specified type.

1. **Set Actions**:

- **Type-Specific Execution**: Actions can be **Formula** or **Value** based.
- **Formula**: Evaluates a formula and updates the target field, treating blank fields as zero if specified.
- **Value**: Uses operators like Add, Subtract, Multiply by, etc., to perform the corresponding arithmetic operation on the target field.
- Specify the Target Object and Source and Target Fields.
- Choose an Operator (e.g., Add, Subtract, Multiply by, Divide by) and set the Value or enter the Formula.
- **Order Field**: The `Order__c` field on the `Rule_Action__c` object determines the execution order for multiple actions. Actions are sorted in ascending order of this field; if `Order__c` is null, the action has the lowest priority and executes last.

## Applying Calculation Rules

Calculation Rules can be triggered in two ways:

1. **Automatically via Work Order Status**:
  - **Trigger**: Rules are applied automatically when a Work Order status is set to "Closed," managed by the `WorkOrderTrigger`.
  - **Note**: This trigger can be disabled if manual application is preferred.
2. **Manually via Button**:
  - **Apply Calculation Rules Button**: Users can manually apply rules by clicking the "Apply Calculation Rules" button on the Work Order page layout.

## Execution Process

1. **Triggering the Process**: When triggered automatically or manually, the system collects relevant **Work Order Line Item** and **Timesheet Lineitem** records and checks them against the Calculation Rules.
2. **Service Agreement Coverage Check**:

- The system verifies Service Agreement Coverage records to see if Work Order Line Items or Timesheet Lineitems are covered.

- Coverage can be based on Installed Products, Locations, or Accounts associated with the Work Order, either directly or indirectly, through various paths:

- The Installed Product linked directly to the Work Order Line Item.
- The Installed Product linked through the Service Request associated with the Work Order.
- The Location associated directly with the Work Order.
- The Location linked through the Installed Product on the Service Request of the Work Order.
- The Account directly associated with the Work Order.

- The Account linked through the Service Request associated with the Work Order.
- If coverage criteria are met, the relevant Service Agreement's Calculation Rules are considered.

1. **Condition Evaluation**: Each record is checked against the Rule Conditions in applicable Calculation Rules.
2. **Action Execution**: For records meeting the conditions, the Rule Actions are executed, updating the records accordingly.
3. **Logging Applied Rules**: A **Matched Calculation Rule** record is created for each affected Work Order Line Item or Timesheet Lineitem, documenting the applied rule.

This setup helps FieldBuddy users automate complex calculations and actions on service records, ensuring consistency and traceability in operations.