nonTransactionalCreation_claude

From the guide Copy your first script.

← ai2fm Guides · Download nonTransactionalCreation_claude.fmscript

# ===============================================================
# nonTransactionalCreation_bare — create 1000 records in myTable
# ===============================================================
# WHAT IT DOES
# Empties myTable, creates 1000 new records in a loop, times the run,
# then shows the records and how long they took.
#
# HOW IT IS BUILT
# - The loop runs on the layout "myTable", which shows no fields: nothing
#   has to be drawn while records are created, so the loop is faster.
# - The script ends on "myTableWithFields", the layout that shows the fields.
# - It is not transactional: every record is saved as the next one is
#   created. If the script stops halfway, the records made so far stay.
#
# NEEDS
# - Full Access: Truncate Table runs only with the Full Access privilege set.
#
# RETURNS
# - A JSON object, {"success":1}, read by the caller with Get ( ScriptResult ).
# ===============================================================

# ----------  SET UP  ----------
# Stop redrawing the window until the script ends.
Freeze Window
Enter Browse Mode [ Pause: Off ]

# How many records to create, and the number of the first one.
Set Variable [ $total[1] ; Value: 1000 ]
Set Variable [ $counter[1] ; Value: 1 ]

# Work on the layout with no fields.
Go to Layout [ “myTable” (myTable) ]
Adjust Window [ Resize to Fit ]

# Delete every record in myTable, with no dialog. This cannot be undone.
Truncate Table [ With dialog: Off ; Table: “myTable” ]

# Start the clock, in milliseconds.
Set Variable [ $startTime[1] ; Value: Get ( CurrentTimeUTCMilliseconds ) ]

# ----------  CREATE THE RECORDS  ----------
Loop [ Flush type: Always ]
    New Record/Request
    # A unique key for the record.
    Set Field [ myTable::PrimaryKey ; Get ( UUID ) ]
    # A readable number, 1 to 1000, set by the script, not by the field.
    Set Field [ myTable::__pkMyTableID ; $counter ]
    Set Field [ myTable::myField ; "auto-generated " & $counter ]
    Set Variable [ $counter[1] ; Value: $counter + 1 ]
    # Stop after record number $total.
    Exit Loop If [ $counter > $total ]
End Loop

# Save the last record. The earlier ones were saved as each new record
# was created.
Commit Records/Requests [ Skip data entry validation ; With dialog: Off ; Force Commit ]

# ----------  REPORT  ----------
# Stop the clock; the difference is turned into seconds.
Set Variable [ $endTime[1] ; Value: Get ( CurrentTimeUTCMilliseconds ) ]
Set Variable [ $elapsedTime[1] ; Value: ( $endTime - $startTime ) / 1000 ]

# Show the records on the layout with the fields.
Go to Layout [ “myTableWithFields” (myTableWithFields) ]
Adjust Window [ Resize to Fit ]

Show Custom Dialog [
    Title: "Success";
    Message: $total & " Records Created at " & $elapsedTime & " seconds";
    Default Button: "OK", Commit: "Yes"
]

Exit Script [
    Result: JSONSetElement (
        "{}" ;
        [ "success" ; 1 ; JSONNumber ]
    )
]