Search code examples
dynamics-crmdynamics-business-centraldynamics-al

Business Central Linked Fields


I am trying to create a "Received" bool field on both the Purchase Lines and the Sales Lines, so that when an item is marked as True on the Purchase Line, the linked Sales Line is also marked as True. So far I have managed to create a field on the Purchase Line to store the id of the associated Sales Line, but I am having a little trouble figuring out the Trigger for when the Purchase Line field is changed and how to use the Sales Line id field to update the specific Sales Line that matches the id.

field(csg_PoliReceived; Rec.csg_PoliReceived)
        {
            Caption = 'Purchase Order Line Received';
            ApplicationArea = All;
            trigger OnValidate()
            begin
                // if true
                // update Sales Line to true
                // if false
                // update Sales Line to false
            end;
        }

I have this snippet in a pageextension that extends the Purchase Lines page, so I am not exactly sure how to query for the Sales Line with the matching Id and how to update that record. Any advice would be much appreciated. Thanks!


Solution

  • The modification should be done in the OnValidate trigger of the field on the tableextension you created for Purchase Line.

    I am going to assume that you have a field called Sales Line Id which would contain the SystemId of the Sales Line if the Purchase Line is linked to one.

    trigger OnValidate()
    var
        SalesLine: Record "Sales Line";
    begin
        if not IsNullGuid("Sales Line Id") then begin
            SalesLine.GetBySystemId("Sales Line Id");
            SalesLine.Validate("Purchase Order Line Received", Received);
            SalesLine.Modify(true);
        end;
    end;
    

    This code will update Purchase Order Line Received on the Sales Line whenever Received is updated on the Purchase Line.

    One thing you might want to consider in your design is that a Purchase Line and a Sales Line can be linked in several ways e.g. through reservation or drop shipment.