Calculated field on Sales Line table

I have created an extension of the table “Sales line”, in which I have created a field “Shipped Not Inv. No VAT”, which should be the result of the multiplication of two existing fields in the table, “Qty. Shipped Not Invoiced” * “Unit price”, this in each line. I have added the operation in the “OnAfterValidate ()” tigger of both fields, but nothing happens.

Also try to do it through a codeunit, following this example:

https://community.dynamics.com/business/f/dynamics-365-business-central-forum/300617/add-dynamically-calculated-field-to-sales-document-lines

But it doesn’t work for me either. Is there something I need to add somewhere?

codeunit 52122 ShipNotInvNoVATPub
{
    trigger OnRun()
    begin

    end;

    [IntegrationEvent(false, false)]
    local procedure OnAfterValidate(var r: Record "Sales Line")
    begin
    end;
}

codeunit 52123 ShipNInvNoVATSub
{
    trigger OnRun()
    begin

    end;

    [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterValidateEvent', 'Unit Price', false, false)]
    local procedure UnitPriceChange(var Rec: Record "Sales Line")
    begin
        Rec."Shipped Not Inv. No VAT" := Rec."Unit Price" * Rec."Qty. Shipped Not Invd. (Base)";
    end;

    [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterValidateEvent', 'Qty. Shipped Not Invd. (Base)', false, false)]
    local procedure QtyChange(var Rec: Record "Sales Line")
    begin
        Rec."Shipped Not Inv. No VAT" := Rec."Unit Price" * Rec."Qty. Shipped Not Invd. (Base)";
    end;


}

As a general rule, I avoid these type of calculated fields. Unless there is an underlying requirement that requires this value to be stored in a physical field. Instead use a function that calculates and returns the desired value.

2 Likes

Field “Qty. Shipped Not Invd. (Base)” is not validated, rather than its value is just set to the field in InitOutstanding function.
Therefore you should subscribe to OnAfterInitOutstanding integration event instead.

2 Likes