KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Duplicating selected records
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: January 18, 2018

Here is a utility method to duplicate a selection of records:

// ----------------------------------------------------------------------
// Name: DUPLICATE_SELECTED_RECORDS
// Description: Method will duplicate the selected record(s).
//
//
// Parameters:
// $1 (POINTER) - Table to pointing to the selection
//
// Output:
// $0 (BOOLEAN) - True - Records created, False - No duplicated records
// ----------------------------------------------------------------------
C_POINTER($1;$ptrTable)
C_BOOLEAN($0;$succes_b)
C_LONGINT($i)
ARRAY LONGINT($arr;0)

If (Count parameters=1)
  $succes_b:=True

  $ptrTable:=$1

  For ($i;1;Records in selection($ptrTable->))
    APPEND TO ARRAY($arr;Record number($ptrTable->))
    NEXT RECORD($ptrTable->)
  End for

  START TRANSACTION

  For ($i;1;Size of array($arr))
    GOTO RECORD($ptrTable->;$arr{$i})
    DUPLICATE RECORD($ptrTable->)
    SAVE RECORD($ptrTable->)

    If (OK=0) // bail out of loop if saving a record fail
      $i:=Size of array($arr)+1
      $succes_b:=False
      CANCEL TRANSACTION
    End if
  End for

  If ($succes_b)
    VALIDATE TRANSACTION
  End if

End if

$0:=$succes_b


Here is an example of duplicating all records:



C_BOOLEAN($status)

ALL RECORDS([Table_1])
$status:=DUPLICATE_SELECTED_RECORDS (->[Table_1]) // True


The result of the duplicated records:




Annother example is duplicating records that contain 'a'':

QUERY([Table_1];[Table_1]Field_2="a")
$status:=DUPLICATE_SELECTED_RECORDS (->[Table_1]) // True

The result of duplicating 'a' records: