Event Sequence - update field on modify and insert

markclemarkcle Member Posts: 65
Hi All

Finalizing my first extension and stuck on one thing.

For certain shipping methods we have to add a freight line (non inventory item) and calc freight as percent of total order value. The percent is a unit price for each customer (e.g. 0.1 = 10%) and we put the order total in qty, so if Order is $1000, freight is $100

I have a subscription to change in Ship method and can insert the line with correct Qty (order total)

I also have a subscription on sales lines, so that if a sales line amount changes and there is a freight line, it will recalculate the total (calls a proc in a code unit)
[EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterModifyEvent', '', true, true)]

I have one use case that doesn't work: The user inserts all line items and at the end, MANUALLY adds the freight line

I subscribed to the SalesLineAfterInsert, which calls my proc in the codeunit
    [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterInsertEvent', '', true, true)]
    var
        // Assign SLO variable to codeunit of SalesLineOperarions
        SLO: Codeunit SalesLineOperations;
        OrderTotal: decimal;
        
     begin
         // Check if this is a FREIGHT line
         if rec."No." = 'FCOM' then begin
            SLO.UpdateFCOMQty(rec);
         end;
   end;


Here's my Proc
  procedure UpdateFCOMQty(var Rec: Record "Sales Line")
    // This is called whenever a sales line is modified or inserted
    // Checks if there's an FCOM record
    // If there is one, sum all the non-freight lines before tax and set this as QTY on FCOM
    var
        OrderTotal: decimal;
        FreightSalesLine: record "Sales Line";
    begin

        // See if there are any FCOM lines. if so, calc total before tax and freight
        FreightSalesLine.SETRANGE("Document Type", Rec."Document Type");
        FreightSalesLine.SETRANGE("Document No.", Rec."Document No.");
        FreightSalesLine.SETRANGE("No.", 'FCOM');

        // If we have an FCOM line, calc the total of order minus freight
        If FreightSalesLine.Find('-') then begin

            OrderTotal := CalcOrderTotalWithoutTaxAndFreight(rec."Document No.", Rec."Document Type");

            FreightSalesLine.Validate(Quantity, Round(OrderTotal, 1));
            FreightSalesLine.Modify(TRUE);
        end;
    end;

I create a new line, enter the item no and hit tab, the proc runs, and all the calculations seem to work correctly, but the cursor is left in the Qty field (next column) and but the qty is not populated.

In most other languages, I would just use the OnLoseFocus event and populate the Qty field.

Can someone point me in the right direction or how to populate a filed of a newly entered record?

Thanks

mark

Best Answer

Answers

  • DenSterDenSter Member Posts: 8,304
    What I would do is create a function at the line level that creates/updates the related charges, and provide the ability to run that from the functions menu on the line. I'd also add a function at the header level so I can run it for all lines. Then, I would add an additional call to this function from the release process to make sure that orders cannot be released without those additional charges.

    I would personally stay away from database triggers, because it is unpredictable what happened when a line was inserted/modified. The user can move the cursor up and down when just selecting the line type, or just selecting the item number without having a quantity/uom/price. Any amount calculation will be false at that time anyway.
  • markclemarkcle Member Posts: 65
    thanks for replies. TallyHo's answer worked (did actually figure it out myself :-))

    Thanks again

    Mark
Sign In or Register to comment.