KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Getting the last record number via SQL
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: October 19, 2020

Getting the last Record ID of a table can be quickly accomplished using the following code:

C_LONGINT($0;lastRecordID)
C_TEXT($1;$sql)
If (Count parameters=1)
   $sql:="SELECT MAX(__ROW_ID) FROM ["+$1+"] INTO :lastRecordID"
   Begin SQL
   execute IMMEDIATE :$sql
   End SQL
   $0:=lastRecordID
End if


If saved in a project method named GetLastRecordID this can be used like this:

alert(string(GetLastRecordID("tableName")))


or

C_LONGINT($recordID)
C_TEXT($tableName)
$tableName:=Table name(->[name])
$recordID:=GetLastRecordID($tableName)