Use the following procedure to define custom price calculations that need to reason about a full collection of reservations and their related records, rather than a single reservation in isolation.
GoMeddo offers two approaches for customising the reservation price: the B25.Util_PluginManager.ReservationPrice interface (see Apply custom price calculations), and the contextual price calculation described on this page. The contextual calculation is the right choice when:
-
Pricing on one reservation depends on other reservations in the same collection, such as its parent, children, or siblings.
-
You need to query other records during the calculation. The contextual calculation lets you bulkify those queries.
Only want to switch on contextual pricing, for example for the new Service setup? You don’t need to write a class. Follow the steps under Activate the custom price calculator class and enter B25.CustomPriceCalculation as the String Value. GoMeddo then applies its standard pricing logic through the contextual calculation.
Create your custom price calculator class
Create a class that extends B25.CustomPriceCalculation. The class must be global so that GoMeddo can instantiate it. In that class, override the calculate method with the following signature:
apex
global override void calculate(List<B25.CustomPriceCalculation.PriceCalculationParameters> calculationRequests)
Each PriceCalculationParameters item in the list represents one reservation that needs to be priced. It exposes the following properties:
|
Name |
Type |
Purpose |
|---|---|---|
|
|
|
Wraps the reservation currently being priced. |
|
|
|
The full collection being processed, exposing |
|
|
|
|
|
|
|
The object on which price changes are recorded. Call |
Why record changes on PriceChanges instead of writing directly to the record? This lets you update fields that are read-only. The new values then appear in the UI and are returned as changes to the REST endpoint.
Activate the custom price calculator class
Register the class so GoMeddo uses it:
-
Go to Setup > Custom Settings.
-
Click Manage next to System Setting.
-
Click New.
-
In Name, enter
Contextual Price Calculation Class. -
In String Value, enter the name of your custom class, for example
InstallationPartnerPriceCalculation. -
Click Save.
As long as this setting exists, GoMeddo calls your calculate method whenever a triggering field changes on a reservation, or on a related record the calculation depends on.
To use the standard pricing logic without custom code, enter B25.CustomPriceCalculation as the String Value.
(Optional) Control which fields trigger the calculation
Override getTriggerConfiguration to customise:
-
Which field changes trigger a price recalculation, including fields on related objects such as Service Reservation
-
Whether a change on a child reservation should also trigger recalculation of the parent
-
Whether a change on a parent reservation should also trigger recalculation of its children
apex
global override B25.CustomPriceCalculation.CalculationTriggerConfiguration getTriggerConfiguration()
The returned CalculationTriggerConfiguration exposes the following properties:
|
Property |
Type |
Purpose |
|---|---|---|
|
|
|
Fields whose changes trigger recalculation. Unlike the |
|
|
|
When |
|
|
|
When |
If you don’t override this method, GoMeddo uses a default trigger configuration:
-
The standard Reservation pricing fields trigger recalculation: Start, End, Base Price, Calculation Method, Quantity, Skip Subtotal Calculation, and any pricing fields configured on dimensions and dimension junctions.
-
The core Service Reservation fields, Quantity and Unit Price, also trigger recalculation, but only when legacy services are enabled.
-
parentCalculationRequiresChildrenis enabled.
Examples
Fixed discount for an Installation Partner
The sample below adds the Account field to the set of triggering fields and sets the subtotal to zero when the linked Account is an Installation Partner. The default calculation runs first via super.calculate, so the standard pricing logic is still applied to every other reservation.
apex
global class InstallationPartnerPriceCalculation extends B25.CustomPriceCalculation {
global override B25.CustomPriceCalculation.CalculationTriggerConfiguration getTriggerConfiguration() {
B25.CustomPriceCalculation.CalculationTriggerConfiguration config = super.getTriggerConfiguration();
config.triggeringFields.add(B25__Reservation__c.B25__Account__c);
return config;
}
global override void calculate(List<B25.CustomPriceCalculation.PriceCalculationParameters> calculationRequests) {
super.calculate(calculationRequests);
Set<Id> accountIds = new Set<Id>();
for (B25.CustomPriceCalculation.PriceCalculationParameters request : calculationRequests) {
if (request.beingProcessed.reservation.B25__Account__c != null) {
accountIds.add(request.beingProcessed.reservation.B25__Account__c);
}
}
if (accountIds.isEmpty()) {
return;
}
Map<Id, Account> accountsById = new Map<Id, Account>([
SELECT Id, Type FROM Account WHERE Id IN :accountIds WITH USER_MODE
]);
for (B25.CustomPriceCalculation.PriceCalculationParameters request : calculationRequests) {
Account linkedAccount = accountsById.get(request.beingProcessed.reservation.B25__Account__c);
if (linkedAccount != null && linkedAccount.Type == 'Installation Partner') {
request.changes.updateFieldValue(B25__Reservation__c.B25__Subtotal__c, 0);
}
}
}
}
Parent subtotal as the sum of its children
The sample below recalculates the parent reservation’s subtotal as the sum of its child reservations’ subtotals. Because child changes must flow up to the parent, parentCalculationRequiresChildren is kept enabled (it is on by default).
apex
global class RollupChildSubtotals extends B25.CustomPriceCalculation {
global override B25.CustomPriceCalculation.CalculationTriggerConfiguration getTriggerConfiguration() {
B25.CustomPriceCalculation.CalculationTriggerConfiguration config = super.getTriggerConfiguration();
config.parentCalculationRequiresChildren = true;
return config;
}
global override void calculate(List<B25.CustomPriceCalculation.PriceCalculationParameters> calculationRequests) {
super.calculate(calculationRequests);
for (B25.CustomPriceCalculation.PriceCalculationParameters request : calculationRequests) {
if (!request.isParent) {
continue;
}
Decimal total = 0;
for (B25.Reservation child : request.collection.childReservations) {
total += child.reservation.B25__Subtotal__c ?? 0;
}
request.changes.updateFieldValue(B25__Reservation__c.B25__Subtotal__c, total);
}
}
}