Code Optimization

BeliasBelias Member Posts: 2,998
  //-SOURCE DOCUMENT = "Purchase Order"
  "Source Document"::"Purchase Order": BEGIN
    PurchInvLine.SETRANGE("OF00 Purch. Order No.","Entry No.");                            //(*)
    PurchInvLine.setrange("posting date","last invoice date");                             //(*)
    PurchInvLine.setrange(type,purchinvline.type::Item);                                   //(*)
    PurchInvLine.setrange("no.","item no.");                                               //(*)
    if PurchInvLine.findset(false,false) then begin
      TBTempPurchInvLine."OF00 Purch. Order No." := PurchInvLine."OF00 Purch. Order No.";
      TBTempPurchInvLine."posting date" := PurchInvLine."posting date";
      TBTempPurchInvLine.type := PurchInvLine.type;
      TBTempPurchInvLine."no." := PurchInvLine."no.";
      repeat
        TBTempPurchInvLine."document no." := PurchInvLine."document no.";
        if TBTempPurchInvLine.insert then begin             //Means that this (*) group of lines haven't been analyzed yet
          tbpurchinvlinetoloop.copyfilters(purchinvline);
          tbpurchinvlinetoloop.setrange("document no.",purchinvline);
          tbpurchinvlinetoloop.findset(false,false);
          repeat
            [Some code]
          until tbpurchinvlinetoloop.next = 0;
        end;
      UNTIL PurchInvLine.NEXT = 0;
    end;
  END;
  //+SOURCE DOCUMENT = "Purchase Order"

Can I do this code better?Personally i like it, but maybe won't perform fine.
Explanation: This is a code snap of a process similar to get item entries in intrastat jnls.
I set some ranges on puchase lines (i can't do setrange("document no.",...)).
then i fill a temptable in order to see which lines i already analyzed and then loop another variable to make some calculations.
All the variables declared belong to record purch inv lines.
The code is written in the aftergetrecord of a record similar to item ledger entry
Last question: does "IF INSERT THEN" instruction have performance overhead with temptables?(I don't think so)
Hope to be clear, thanks in advance
-Mirko-
"Never memorize what you can easily find in a book".....Or Mibuso
My Blog

Answers

  • DenSterDenSter Member Posts: 8,304
    On a temp variable that sholdn't make any performance impact, it's just in memory.

    I'm not getting the purpose of your code. Why are you looping through the same invoice lines in two different loops? First you loop through PurchInvLine, and then if certain conditions are met, you set the same filters on tbpurchinvlinetoloop, both if which look like they are Purchase Invoice Lines.
  • BeliasBelias Member Posts: 2,998
    On a temp variable that sholdn't make any performance impact, it's just in memory.
    as i tought, now i'm sure thanks

    anyway,after the first set of filters, let's say we have these records (i filter order no = 1)

    docNo OrdNo quantity
    a 1 12
    a 1 13
    b 1 1
    b 1 5
    c 1 6
    c 1 66

    this is the [Some Code] i wrote in the previous post
                    TotalInvoicedQty := TotalInvoicedQty + TBSalesInvoiceLinetoloop.Quantity;
    
    i have to group the records in order to have in totalinvoicedqty the sum of "a" document quantity (25) and then run a function. Obviously, i need to do this only one time per document, so i use "if insert" in order to insert only one line for the (*) fields if the "if insert" returns false, then it means that i already processed the document.
    It's a bit intricated (does this word exists?), i know...
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • DenSterDenSter Member Posts: 8,304
    Ah so you're keeping track of the value of the document number really. I would personally use a simple variable (like a Code20) for that. Sort the records by that number, and every time it changes, the grouping changes.

    Apart from the fact that you could do sumindexfields for totals (which is much easier to do), it's not that hard to code for it. Sort the lines by the order number (you might have to create a key for it), so you only need one loop. Instead of the temp record variable, have a variable of the same type as the order number. Then it's a straightforward loop, something like this:
    //-SOURCE DOCUMENT = "Purchase Order"
      "Source Document"::"Purchase Order": BEGIN
        PurchInvLine.SETCURRENTKEY("OF00 Purch. Order No."); // to sort it by this field
        PurchInvLine.SETRANGE("OF00 Purch. Order No.","Entry No."); 
        PurchInvLine.setrange("posting date","last invoice date"); 
        PurchInvLine.setrange(type,purchinvline.type::Item); 
        PurchInvLine.setrange("no.","item no."); 
        if PurchInvLine.findset(false,false) then begin
          MyOrderNumber := ''; // to initialize the variable
          repeat
            IF PurchInvLine."OF00 Purch. Order No." = MyOrderNumber THEN BEGIN
              // the order number is the same as the number on the previous line
              TotalInvoicedQty := TotalInvoicedQty + PurchInvLine.Quantity; // add to the total
            END ELSE BEGIN
              // the number has changed since the last line, so start fresh
              TotalInvoicedQty := PurchInvLine.Quantity;; // sets the total quantity to the current value
            END;
            MyOrderNumber := PurchInvLine."OF00 Purch. Order No."; // to compare the next order number to the current
          UNTIL PurchInvLine.NEXT = 0;
        end;
      END;
      //+SOURCE DOCUMENT = "Purchase Order"
    
    It still doesn't make sense to me though. If you filter it by one value of the order number, it's never going to change. Perhaps you are not posting all the code, so in that case, substitute my control variable with the one that does change on your project and see if that does it.
  • BeliasBelias Member Posts: 2,998
    DenSter wrote:
    Ah so you're keeping track of the value of the document number really. I would personally use a simple variable (like a Code20) for that. Sort the records by that number, and every time it changes, the grouping changes.

    Apart from the fact that you could do sumindexfields for totals (which is much easier to do), it's not that hard to code for it. Sort the lines by the order number (you might have to create a key for it), so you only need one loop. Instead of the temp record variable, have a variable of the same type as the order number. Then it's a straightforward loop, something like this:
    //-SOURCE DOCUMENT = "Purchase Order"
      "Source Document"::"Purchase Order": BEGIN
        PurchInvLine.SETCURRENTKEY("OF00 Purch. Order No."); // to sort it by this field
        PurchInvLine.SETRANGE("OF00 Purch. Order No.","Entry No."); 
        PurchInvLine.setrange("posting date","last invoice date"); 
        PurchInvLine.setrange(type,purchinvline.type::Item); 
        PurchInvLine.setrange("no.","item no."); 
        if PurchInvLine.findset(false,false) then begin
          MyOrderNumber := ''; // to initialize the variable
          repeat
            IF PurchInvLine."OF00 Purch. Order No." = MyOrderNumber THEN BEGIN
              // the order number is the same as the number on the previous line
              TotalInvoicedQty := TotalInvoicedQty + PurchInvLine.Quantity; // add to the total
            END ELSE BEGIN
              // the number has changed since the last line, so start fresh
              TotalInvoicedQty := PurchInvLine.Quantity;; // sets the total quantity to the current value
            END;
            MyOrderNumber := PurchInvLine."OF00 Purch. Order No."; // to compare the next order number to the current
          UNTIL PurchInvLine.NEXT = 0;
        end;
      END;
      //+SOURCE DOCUMENT = "Purchase Order"
    
    It still doesn't make sense to me though. If you filter it by one value of the order number, it's never going to change. Perhaps you are not posting all the code, so in that case, substitute my control variable with the one that does change on your project and see if that does it.

    yes, "document no." is the one that changes, not OF00, sorry but i didn't specify it (document no. is a primary key field of purchase line and I don't set the other pk fields, so "if insert" instruction will return false unless the document no. changes)

    anyway, your code is simpler, (i used your method in the past, why didn't i think about it? ](*,) ](*,) ](*,)
    thanks a lot
    -Mirko-
    "Never memorize what you can easily find in a book".....Or Mibuso
    My Blog
  • DenSterDenSter Member Posts: 8,304
    Right I figured I wasn't looking at the right field :mrgreen: Glad to be able to help in any way. Let us know when you get this thing to work.
Sign In or Register to comment.