Max () can “only” process one or more fields of one record, or a set of related records, or a list of literal numeric values (or any combination thereof), but it does not work across a found set. For this you need a summary field (see below).
Your script step reads the “maximum’ of the field in the record you happen to be on after the import (probably the first one), which is simply the value itself.
To get the maximum value across a found set (all records, in your case), create a field of type summary, say 'sMaxOfPrimaryKeyfield’, and define it as Maximum of PrimaryKeyField.
To use it in your script:
Show all Records
Set Next Serial Value [ Example::PrimaryKeyField ; GetSummary ( sMaxOfPrimaryKeyfield ; sMaxOfPrimaryKeyfield ) + 1 ]
I have solved this a couple of different ways in the past:
1. Run a script at the end of the import to:
Show all Records
Sort on the serial number field (ascending)
Go to last record
Set Next Serial Value (this record's serial number +1)
2. Establish an X-type self-join relationship for the table, and have that relationship SORTED (Descending) in the relationship definition. Then, from any record, the value returned for the first related record will be the largest existing Serial number, and you can use the same basic script step to reset the serial value:
Set Next Serial Value (related serial number +1)
This method requires creating the sorted relationship, but allows you to leave the imported set as the found set without having to show all records or sort them.
An alternative similar to Stephen's option 2 is to then use the Max () function pointed through this relationship. That way the Set Next Serial Value step can be used as in the original script.
erolst, thank you! You are the best, it worked. Here is the revised script that I used for my solution.
Go to Layout ["Example Layout" ( Example )]
Import Records [ No Dialog; "DeployedDatabase.fmp12"; Add; Windows ANSI]
Show All Records
Set Next Serial Value [ Example::sMaxOfPrimaryKeyField; Max ( Example::sMaxOfPrimaryKeyfield ) + 1 ]
Show Custom Dialog ["Import Complete"; "Import has completed"]