Copy a section of a record to another record ind Dynamics NAV

1.3k Views Asked by At

I have a record with let's say 100 lines, that a user marks. I retrieve the record like this:

CurrPage.SETSELECTIONFILTER(recSelection);
recSelection.FINDSET;

Now, I want to copy 20 lines of the selection at a time into another record variable and pass that to a function.

How can i process a record in steps? something like this:

batchSize := 20;
currSize := 0;
totalSize := recSelection.COUNT;
totalProcessed := 0;
recSelection.FINDSET;
REPEAT
  IF (currSize = 0) THEN BEGIN
    tmpRec.INIT;
  END;

  // how can I add the current to the tmp?
  // tmpRec.INSERT -> recSelection

  currSize += 1;
  totalProcessed += 1;

  IF (currSize = batchSize) OR (totalProcessed = totalSize) THEN BEGIN
     SomeHeavyFunction(tmpRec);
     currSize := 0;
  END;

UNTIL recSelection.NEXT = 0;
2

There are 2 best solutions below

0
On

I got it to work by this:

(deleteall with temp records are always a bit scary for me :P), recTemp is a Temporary record

CURR_BATCH_SIZE := 0;
TOTAL_ITEMS := recSelected.COUNT;
BATCH_SIZE := 20;
TOTAL_PROCESSED := 0;
IF recSelected.FIND('-') THEN
REPEAT

  IF (CURR_BATCH_SIZE = 0) THEN BEGIN
    recTemp.RESET;
    recTemp.DELETEALL;
    recTemp.INIT;
  END;

  // copy over
  recTemp := recSelected;
  recTemp.INSERT;

  // store state
  CURR_BATCH_SIZE := CURR_BATCH_SIZE + 1;
  TOTAL_PROCESSED := TOTAL_PROCESSED + 1;

  // did we reach batch limit or end?
  IF (CURR_BATCH_SIZE = BATCH_SIZE) OR (TOTAL_PROCESSED = TOTAL_ITEMS) THEN BEGIN
    // process batch
    MESSAGE('Processing next %1. total processed %2/%3', FORMAT(recTemp.COUNT), FORMAT(TOTAL_PROCESSED), FORMAT(TOTAL_ITEMS));

    // reset counters
    CURR_BATCH_SIZE := 0;    
  END;


UNTIL recSelected.NEXT = 0;
0
On

Depending on your version you could check for recTemp.ISTEMPORARY.

See the documentation for more details.