Skip to content

PreparedTrigger_MaintainValueHistory

github-actions[bot] edited this page Dec 6, 2026 · 3 revisions

Use PreparedTrigger_MaintainValueHistory<TSource, THistory> to record each day's last changed value in UTC. Import Blueprint.Entities.Triggers.PreparedTriggers and place this method inside Product.

/// <summary>Defines the daily history of changes to the product's net price.</summary>
/// <returns>The prepared value-history trigger.</returns>
public override List<BlueprintEntityTrigger> getDatabaseTriggers()
{
    return [new PreparedTrigger_MaintainValueHistory<Product, ProductPriceHistory>(
        name: "TR_Products_DailyNetPriceHistory",
        sourceValue: nameof(NetPrice),
        historyForeignKey: nameof(ProductPriceHistory.ProductID),
        historyDate: nameof(ProductPriceHistory.PriceDate),
        historyValue: nameof(ProductPriceHistory.NetPrice))];
}

Register both entities. Give the history entity a required foreign key to the source ID, a mapped DateOnly column, a unique index on those two columns, and a single writer rule. Use the same SQL type for both value columns, and make the history value nullable if the source value is nullable.

History starts with the first update that changes the value, including a change to or from null. The trigger writes the new value for today in UTC and fills a missing yesterday row with the value just before the update. Further changes on the same UTC date replace today's value and preserve any existing yesterday row. These writes run in the source update's transaction. Installation creates a missing trigger and preserves existing history rows.

For decimal values, add minimumTarget: nameof(Lowest30DaysNetPrice) and minimumDays: 30 to store the minimum on the source entity. Declare the nullable decimal property with [DatabaseGenerated(DatabaseGeneratedOption.Computed)] and the same SQL precision and scale as the source value. The window includes the current value for today and the recorded values from the preceding 29 UTC dates. The calculation ignores null values and stores null if there is no value in the window.

A new source row has empty history and a null minimum until its watched value changes. The passage of time alone does not run this trigger.

Entity triggers and common imports

Clone this wiki locally