It's not clear to me exactly what you are doing, since you refer to importing a "file". Thereafter your description sounds like you are talking about importing a bunch of records. Can you please clarify?
If I understand your situation correctly, you start with
You want to import this and generate
Bat SKU Widget Sub-batch
1 P242 bolt 1-P242
1 P297 nut 1-P297
1 M448 washer 1-M448
1 P297 nut 1-P297
If this is accurate, then you don't need to sort the records after importing. The imported records are in the current found set.
Perform two Replace Field Contents script steps.
The first is set the Batch field to the value of 1 for every imported record.
The second would be a calculated result that is
Batch & "-" & SKU
The next time you import a file, you want to assign the records to be Batch = 2.
While you could store the batch number of the previous import in a field with its storage setting as Global and increment it for the next import, you may want to create a new table that is dedicated to tracking imports. Use a serial number field Batch that auto-increments for each new record. Generating new batch numbers this way will let you keep track of the import date, user who imported, and perhaps some statistics about the import (e.g. # of records, # of sub-batches).
Generating new batch numbers by generating new records in new table is the way you would want to do it in a multi-user environment.
Thanks for your help! That's exactly what I need to do. Ok, so I think I understand. In order to have it automatically increment the batch number, I need to have another table for the batch numbers. I can have the script automatically add a new record at the end of each run. But how do I specify that it sets the batch number to the last record created in that field?
Ok, so I got it to work by creating two fields in the Import tracking table, one called "Last Batch Number" which is global for all the records, and another called "Number for This Batch" which is not global, and keeps the same number that the batch has. Is there a better way to do this, or is this way fine?
Say you have a new table called "Batch".
For records in this table you have a field called BatchNumber which is set to be an auto-enter with a Serial number.
You have a script that is responsible for importing a new file to your ProductActivity table (where each record has SKU, BatchNumber, and SubbatchNumber)
That script might go something like this.
Go To Layout ["Batch table layout"]
Enter Browse Mode
# remember the batch number of the new record to use on the newly imported records
Set Variable [$theBatchNumber; Value: BatchNumber]
Go To Layout ["ProductActivity table layout"]
# Do your own error handling to make sure the import was successful.
Replace Field Contents [ProductActivity::BatchNumber; $theBatchNumber]
Replace FIeld Contents [ProductActivity::SubbatchNumber; $theBatchNumber & "-" & SKU]
Thanks that works perfectly!