Code Optimization

Belias
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
0
Answers
-
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.0 -
On a temp variable that sholdn't make any performance impact, it's just in memory.
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 postTotalInvoicedQty := 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...0 -
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.0 -
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 lot0 -
Right I figured I wasn't looking at the right field
Glad to be able to help in any way. Let us know when you get this thing to work.
0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions