Is there a more optimal way to do this

markclemarkcle Member Posts: 65
Hi All

I'm writing my first extension that we'll use in productions. basically, I need to set to clear the qty to ship on sales lines, but for a blanket sales order, I need to go and find all the sales orders linked the blanket line and sum up the Qty on sales orders.

I tried with calcsums, but it complained about not being able to convert from boolean to int (or something along those lines), even though I was useing ReleaseSalesLines.CALCSUMS(Quantity)

I ended up looping through all the linked sales lines. Doesn't seem optimal to me (see about 90% of way down where is says "// Iterate over all sales orders and find the total qty on sales orders (Qty Ordered: Don't care if it's shipped)"
codeunit 50001 SalesLineOperarions
{


    trigger OnRun()
    begin

    end;

    procedure UpdateLines(DocNo: Text; DocType: Option; Zero: Boolean)
    var
        // Local var
        SalesLine: record "Sales Line";
        ReleasesSalesLine: record "Sales Line";
        BlanketReleaseQty: Integer;
    begin
        //Message('Updating Lines for Order no: %1, Doc Type: %2, Zero: %3', DocNo, DocType, Zero);
        // Loop thruugh all lines for this DocNo and Type where line is an item and Qty to ship is <> 0
        SalesLine.SETRANGE("Document Type", DocType);
        SalesLine.SETRANGE("Document No.", DocNo);
        SalesLine.SETRANGE(Type, SalesLine.Type::Item);
        if Zero = true then begin
            SalesLine.SETFILTER(Quantity, '<>0');
        end;

        IF SalesLine.FINDSET(TRUE) THEN
            REPEAT
                // Regular Orders
                // Validate record
                if (Zero = true)
                then
                    // Message('Line: %1, Qty: %2, New Qty to Ship, %3', SalesLine."Line No.", SalesLine."Quantity", 0);
                    SalesLine.VALIDATE("Qty. to Ship", 0)
                else
                    // Message('Line: %1, Qty: %2, Qty Shipp::ed, %3, Qty Left: %4', SalesLine."Line No.", SalesLine."Quantity", SalesLine."Quantity Shipped", SalesLine."Quantity" - SalesLine."Quantity Shipped");
                    if (DocType = 1) then // Sales Order
                        SalesLine.VALIDATE("Qty. to Ship", SalesLine."Quantity" - SalesLine."Quantity Shipped")
                    else // Blanket
                    begin
                        // If it's a blanket find regular sales lines where Line.BlanketOrderNo = DocNo and line.BlanketLineNo = LineNo
                        ReleasesSalesLine.SETRANGE("Document Type", 1);
                        ReleasesSalesLine.SETRANGE("Blanket Order No.", DocNo);
                        ReleasesSalesLine.SETRANGE("Blanket Order Line No.", SalesLine."Line No.");
                        ReleasesSalesLine.SETRANGE(Type, SalesLine.Type::Item);
                        BlanketReleaseQty := 0;
                        // Iterate over all sales orders and find the total qty on sales orders (Qty Ordered: Don't care if it's shipped)
                        IF ReleasesSalesLine.FINDSET(TRUE) THEN
                            REPEAT
                                BlanketReleaseQty := BlanketReleaseQty + ReleasesSalesLine.Quantity
                            UNTIL ReleasesSalesLine.NEXT = 0;
                        SalesLine.VALIDATE("Qty. to Ship", SalesLine."Quantity" - BlanketReleaseQty);
                    end;
                // execute the OnModify() trigger
                SalesLine.MODIFY(TRUE);
            UNTIL SalesLine.NEXT = 0;
    end;

Thanks

Mark

Answers

  • krikikriki Member, Moderator Posts: 9,115
    [Topic moved from 'Design Patterns (General & Best Practices)' forum to 'NAV Three Tier' forum]

    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • lubostlubost Member Posts: 628
    1. Use Quantity (Base) fields instead of Quantity to avoid different measure units
    2. Try use decimal parameter (e.g. TestQty) instead of Zero - more flexible
    SalesLine.SETFILTER(Quantity, '<>%1', TestQty);
    SalesLine.VALIDATE("Qty. to Ship", TestQty)
    3. Try to use ReleasesSalesLine.CALCSUMS(Quantity) with appropriate index change to avoid loop
Sign In or Register to comment.