Using buffer in codeunit 80-90

Hi all, in codeunit 80 and 90 they use some buffers to group sales/purchase lines. To charge the buffer they use the instruction […] InvPostingBuffer[2] := InvPostingBuffer[1]; IF InvPostingBuffer[2].FIND THEN BEGIN […] My question is: for what reason they use an array? Is a performance issue? Another way to charge the buffer (not using an array) could be the GET instruction, for example: IF NOT InvPostingBuffer.GET(Field1…) THEN BEGIN InvPostingBuffer.INIT; […] InvPostingBuffer.INSERT; END ELSE BEGIN […] InvPostingBuffer.MODIFY; END; A bad aspect in using this method is that the field list in the GET instruction is very long, but I don’t know if there are other reasons a part this. Thanks Marco

The reason why an array of a record is used is because the buffer tables are temporary tables. Normally with temporary tables you can only have one veiw of the data at a time. But by using arrays you can have multiple (in this case 2) views of the data. Don’t forget that data in a temporary table is never insered into the database and only exists in the instance of the varable created. Paul Baxter

Yes, but also using GET it works. So why using the array instead?

Hi marco, One reason for using a second variable is because a lot of times when changing records sent to a function by Reference it is better to use a second varible for the changes. If there are filters on the record you might have unexpected results when changing the fields with filters. Also by using Find instead of Get the code will work regardless if you add some new field to the primary key (which I did several times). Using Get you will actually have to change the code. Regards, Cristi Nicola

If you look at the code […] InvPostingBuffer[2] := InvPostingBuffer[1]; IF InvPostingBuffer[2].FIND THEN BEGIN InvPostingBuffer[2].Amount := InvPostingBuffer[2].Amount + InvPostingBuffer[1].Amount; […] InvPostingBuffer[2].MODIFY; END ELSE InvPostingBuffer[1].INSERT; You InvPostingBuffer[1] is filled in but not inserted. This test to see if there is already a record with the same primary key. Even if you use a GET you will need to use an array and you will still need two references to the same temporary table. But may be if it was changed to InvPostingBuffer[2] := InvPostingBuffer[1]; IF NOT(InvPostingBuffer[2].INSERT) THEN BEGIN InvPostingBuffer[2].FIND; InvPostingBuffer[2].Amount := InvPostingBuffer[2].Amount + InvPostingBuffer[1].Amount; […] InvPostingBuffer[2].MODIFY; END Would involve less processing, but as ever there is more than one way to do things in Navision, and which ever way you do it some one will say why is it done like that. Paul Baxter Edited by - triff on 2002 Jun 13 17:43:52

A nice feature when you have a lot of ‘code’ to run through, and your buffer has to collect amounts many different places in the code. Not before you arrive to the point where the buffer has to be inserted you will know if it possible to make an insert or you have to add the different amounts to the previous inserted record. When using the array you will have the amounts collected in the TempRecord[1] then you transfer this record to TempRecord[2] and if an inserted is not possible you can add up the TempRecord[1] amounts with the TempRecord[2] amounts. That’s it…. Preben Borg, Munk IT A/S