Duplicate record adds the duplicated record to the end of the current found set. If you have more than one record present when this script runs, Go To Record/Request/Page [Previous] then moves you to the record that was last before this script started up.
Add this code:
Set Variable [$RecordNumber ; Get ( RecordNumber)]
Set Variable [$invoice_ID...
Go To Record/Request/Page [no dialog ; $RecordNumber] //use the by calculation option
After the duplication loop, change Go To Record/Request/page [next] to: Go To Record/Request/page [last] so that the correct invoice fields are cleared.
I was hioping you'd see this, since it's yours =)
Adding these steps and changing to Go To Record to "last" duplicated the invoice and ignored the set field; "" steps in the second half of the script. It also failed to duplicate line items from the invoice.
Where did you put Go To Record [Last]?
This should be placed after:
Go To Layout [Invoices]
Go To Record/Request/Page [Last]
I see the problem.
Set Variable [$RecordNumber ; value: Get ( RecordNumber )]
When you should have:
Set Variable [$RecordNumber ; Value: Get ( RecordNumber)]
The way you have it, you are storing the record number of the newly created invoice record instead of the record number of the original invoice record.
I switched it, but the line items are still not appearing and the contact info isn't clearing...
For some reason, the If step is evaluating as false when it should be true.
Here's the script I just tested and confirmed that it works. Compare it to yours and see if you can spot any discrepancies.
Set Variable [ $RecordNumber; Value:Get ( RecordNumber ) ]
Set Variable [ $InvoiceID; Value:Invoices::InvoiceID ]
Go to Record/Request/Page [ $RecordNumber ] [ No dialog ]
If [ not IsEmpty ( LineItems::InvoiceID ) /* Line item records exist */ ]
Go to Related Record [ From table: “LineItems”; Using layout: “LineItems” (LineItems) ] [ Show only related records ]
Go to Record/Request/Page [ First ]
Set Field [ LineItems::InvoiceID; $InvoiceID ]
Go to Record/Request/Page [ First ]
Exit Loop If [ Get ( FoundCount ) = 0 ]
Go to Layout [ original layout ]
Go to Record/Request/Page [ Last ]
Set Field [ Invoices::ShippingDate; "" ]
Note: I only clear one field, just enough to make sure it works and I moved this step outside of the If-EndIF block so that it will be cleared even when there are no line items present to duplicate.
AHA! In my script, Set Variable [RecordNumber... was switched with duplicate record/request.
BTW, be careful with how you sort these invoice records. Due to Filemaker's infamous "auto sort" feature that cannot be easily disabled, the newly duplicated record could jump to a different location in the found set or, it could stay adjacent to the record from which it was duplicated. (New records and records where a field specified in the sort order is edited, will "jump" to the position in the found set consistent with the currently specified sort order after the changes are committed.)
The script you now have should work fine as long as the records are unsorted and unsorted records will be in the order they are created--a logical order for invoices in most cases--so you may want to "unsort" the records before performing this script. (Sorting the invoice records by Invoice_ID should also be OK as the new record will have the largest Invoice_ID and that will put it at the end of the found set as expected.)
I'll keep that in mind. It's very rare that anyone using the duplication function would need to sort records. I'm the only one that ever sorts records and I almost never use the function. Luckily, we're a small company so even if it did auto sort, it would be easy to find the descrepancy. The auto sort feature is something we've actually had trouble with in our inventory though. I've had to fight with it to edit some records that keep jumping around.
One trick that I learned here in the Forum, is to start up a script and then pause it. As long as it is paused, the auto-sort doesn't kick in, but you can manually edit the records. After editing, you can cancel or continue the paused script.
I noticed that the script sometimes duplicates everything perfectly in the same order and other times it reverses the order of the line items. So for example, on some of duplications instead of duplicating 1 Item, 2 Item, 3 Item it orders them 3 Item, 2 Item, 1 Item. For us, this is a problem becuase of the way we order line items. Everything needs to be uniform. Drum kits first, then amps, then keyboards, etc..
Any ideas why it might be doing this?
Not sure. Check sort order settings.
Is there a sort order specified in your line item portal? As written, the line item records are duplicated in the order they were originally created (since there's no sorting of records after Go To Related Records pulls them up.) or in the sort order specified in the relationship (this is done in Manage | Database | Relationships). This could conceivably produce a different order when the records are displayed in your portal, but I can't tell from here.
Simplest "fix" might be to specify a sort order in your line items portal that always sorts the records displayed in it in the desired order no matter what order they were created in.
I don't have any sorts enabled. I didn't touch the loop portion of the script from before this post when it was duplicating correctly each time.