---
schema_version: "1"
id: f33d7630b11b987575bcc6cf3517975c08b2a70b29d844973b8d452712b07842
title: Declarative Calculation Procedures (Managed Package)
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/xcloud/264-0-0/os_declarative_calculation_procedures.md
canonical_url: https://help.salesforce.com/s/articleView?id=xcloud.os_declarative_calculation_procedures.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: 3455696db854817db5c3b0a5620686058f285bc29fbe25918eb4f15f9325f2eb
description: For the managed package runtime, declarative calculation procedures are a key component of the ratings engine. Along with calculation matrices, calculation procedures are the key components of any or ratings engine. Calculation procedures take an input JSON and run through a series of matrix lookups (calling values from calculation matrices) and calculations to calculate ratings for policy products.
products:
  - Omnistudio
release: "264"
---
# Declarative Calculation Procedures (Managed Package)

For the managed package runtime, declarative calculation procedures are a key component of the ratings engine. Along with calculation matrices, calculation procedures are the key components of any or ratings engine. Calculation procedures take an input JSON and run through a series of matrix lookups (calling values from calculation matrices) and calculations to calculate ratings for policy products.

![Managed Package app icon](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/resources-global/_corporate/images/managedpackageicon.png) This information is for Omnistudio for Managed Packages. For Omnistudio on standard runtime, see [Omnistudio Help](https://help.salesforce.com/s/articleView?id=xcloud.os_omnistudio_standard.htm&language=en_US).

![An OmniScript, Integration Procedure, or other component calls the Calculation Procedure and passes in JSON data. The Calculation Procedure calls Calculation Matrices and performs calculations in its steps. When it completes, the Calculation Procedure returns JSON data to the calling component.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/omnistudio_for_managed_packages/images/ratingsengine_basic.png)

Calculation procedures can calculate coverage premiums, insured party premiums, insured item premiums, and insurance policy premiums. Calculation procedures can calculate attribute-based prices for products.

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/images/icon_note.png)

Note

Beginning with the Summer '19 release, declarative calculation procedures support:

- Grouped calculation matrices
- Row-versioned calculation matrices

## What's in a Calculation Procedure

A well-formed calculation procedure contains the following elements:

- Constants
    
    All the constants you need for each calculation step
- Variables
    
    Input variables, all the variables you need for each calculation step and the group key and subgroup key if you're calling a group of matrices all the variables in calculation matrices you call in matrix lookup steps
- Calculation steps, including:
    
    - Calculations
        
        Each calculation required to get to the correct result of the whole calculation procedure
    - Matrix lookups
        
        Finds one or more variable values in calculation matrices, based on one or more variable inputs
- Aggregation steps (optional)
    
    If this calculation procedure will handle an array of inputs, aggregation steps will perform calculations over the output sets.

## How Calculation Procedures Work in Context

Omniscripts, Integration Procedures, and other Vlocity processes can call a calculation procedure. The calculation procedure takes a JSON that gives the calculation procedure the ingredients it needs to complete its calculation.

![An OmniScript, Integration Procedure, or other component calls the Calculation Procedure and passes in JSON data. A preprocessor Apex class runs before the Calculation Procedure steps start. The Calculation Procedure calls Calculation Matrices and performs calculations in its steps. After the steps, a postprocessor Apex class runs. Then the Calculation Procedure returns JSON data to the calling component.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/omnistudio_for_managed_packages/images/calcprocedure_context.jpg)

Calculation procedures can take a single input, a single set of inputs, or multiple arrays of inputs.

Here's an example of a single set of input values, as passed in by a JSON:

```
{
   "input": {
      "opMVRPoints": "0",
      "opAccidentPoints": "0",
      "driverGoodStudent": "N",
      "driverMaritalStatus": "M",
      "driverGender": "M",
      "driverYrsExp": "14",
      "driverMileage": "7500",
      "driverUse": "Business"
   }
}
```

The calculation procedure runs through all its steps once for a single input or single array of inputs. It returns a calculation or array of calculations. The output JSON for the calculation procedure that processed this input is:

```
{
   "output": [
      {
         "calculationResults": [
            {
               "driverCCD": 1.11,
               "driverCOLL": 0.92,
               "driverMED": 0.91,
               "driverUM": 0.92,
               "driverBIPD": 0.86,
               "autoSafeDriver__driverSafe": "Y",
               "ID": "input"
            }
         ],
         "aggregationResults": {}
      }
   ]
}
```

Because the calculation procedure did not have any aggregation steps, there are no aggregation results in the output JSON.

## How Aggregation Works in Calculation Procedures

When you add aggregation steps to your calculation procedure, the calculation procedure puts together the results of each run through itself, performs operations on them, then outputs the results of the operations. Here's what that looks like:

![An OmniScript, Integration Procedure, or other component calls the Calculation Procedure and passes in multiple sets of JSON data. The Calculation Procedure runs once for each set of inputs, calling Calculation Matrices and performing calculations in its steps in each run. All the runs are aggregated, then the Calculation Procedure returns JSON data to the calling component.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/omnistudio_for_managed_packages/images/calcprocedure_functionality.jpg)

For example, in a simple case, the calculation procedure needs to calculate a group premium based on a census. Each census member has a set of input values that's passed to the calculation procedure. The calculation procedure runs all of its calculation steps for each member, resulting in an output (premium) for each member. So now it's got an array of outputs. To sum the premiums of all the members of the census, an aggregation step sums all the premium output variables.

![Note](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/images/icon_note.png)

Note

The `aggByKey` functionality in the following example is available in Vlocity Insuranceand Vlocity Health only.

In a more complex scenario, an auto insurance calculation procedure must calculate a premium per instance of car and driver. For example, a user needs to rate out a policy for two cars—a Toyota Prius and a Tesla Model S. The Prius is driven by Roger and Amy, and the Model S is driven by Amy and Steve. The calculation procedure needs to calculate and aggregate results separately for the Prius and for the Model S.

In this case:

- `instanceKey` = car+driver
- `aggByKey` = carModel

To complete this calculation with aggregation, the calculation procedure uses `aggByKeys`. When the calculation procedure receives `aggByKeys` as an input, it runs through the aggregation steps once per `aggByKey`. Here's what that looks like:

![An OmniScript, Integration Procedure, or other component calls the Calculation Procedure and passes in multiple sets of JSON data. The Calculation Procedure runs once for each set of inputs, calling Calculation Matrices and performing calculations in its steps in each run. All the runs are aggregated, but the aggregation has subgroups based on aggByKey values. Then the Calculation Procedure returns JSON data to the calling component.](https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-xcloud-omnistudio_for_managed_packages-264-0-0-production-enus/aa2554ec-624a-4ee6-8da4-6435670e6e62/omnistudio_for_managed_packages/images/calcprocedure_aggbykey.jpg)

So for the example above, the calculation procedure groups the results of its four calculation step runs into two subsets: one for the instances for the Prius and another for the instances for the Model S. Then it runs the aggregation steps twice: once for the Prius and again for the Model S. The calculation procedure returns two aggregation results—one for each vehicle.

You don't need to add `aggByKeys` and instanceKeys to your calculation procedure as variables. As long as the `aggByKey` option and `includeInputKeys` option are set correctly in the services, the `getRatedProduct` and `repriceProduct` services will pass the keys in automatically as part of the input JSON.

## Preprocessor and Postprocessor Classes

These are Apex classes that you can use if you have to massage your input data or output data before and after it's processed by a calculation procedure. See [Preprocessor Class Example (Managed Package)](https://help.salesforce.com/s/articleView?id=xcloud.os_preprocessor_class_example.htm&release=264.0.0&type=5 "For the managed package runtime, the following example shows the typical structure of a preprocessor Apex class for a declarative Calculation Procedure.").

If you think you need to use preprocessor classes or postprocessor classes, please contact your Vlocity representative to verify that your use case requires that you write these classes. (We have a lot of tools that are easier and more configurable now, and some of those might work better for you.)

- **[Workflow for Creating a Calculation Procedure (Managed Package)](https://help.salesforce.com/s/articleView?id=xcloud.os_workflow_for_creating_a_calculation_procedure.htm&release=264.0.0&type=5)**  
    For the managed package runtime, to create a calculation procedure, you set up the overall configuration, add variables and constants, add steps, and test.
- **[Create a New Version of a Calculation Procedure (Managed Package)](https://help.salesforce.com/s/articleView?id=xcloud.os_create_a_new_version_of_a_calculation_procedure.htm&release=264.0.0&type=5)**  
    For the managed package runtime, it's much quicker to create a new version of an existing calculation procedure than it is to create a new version from scratch. When you create a new version, all the details, variables, constants, calculations, matrix lookups, and aggregation steps are cloned from the previous version.
- **[The Looping Feature in Calculation Procedures (Managed Package)](https://help.salesforce.com/s/articleView?id=xcloud.os_the_looping_feature_in_calculation_procedures.htm&release=264.0.0&type=5)**  
    For the managed package runtime, looping calculations take a starting point, an endpoint, and an interval. At run time, the calculation procedure runs in a loop from start to end per interval. Each run uses the same input variables, plus an `index` variable that you can use in the calculation.
- **[Configure a Calculation Procedure for Looping (Managed Package)](https://help.salesforce.com/s/articleView?id=xcloud.os_configure_a_calculation_procedure_for_looping.htm&release=264.0.0&type=5)**  
    For the managed package runtime, make sure that your calculation procedure UI is set up for looping calculations, then configure looping variables.
- **[Preprocessor Class Example (Managed Package)](https://help.salesforce.com/s/articleView?id=xcloud.os_preprocessor_class_example.htm&release=264.0.0&type=5)**  
    For the managed package runtime, the following example shows the typical structure of a preprocessor Apex class for a declarative Calculation Procedure.