Well we can't see what criteria you are using in your find steps so we can't see how that might fail. It for DoorPreps, it looks like you might get this result if the Find fails to find any records matching the stored criteria.
And for CopyPreps, your script simply changes layouts. DeleteAll Records will delete whatever records happen to be in the current found set, there doesn't appear to be anything in your script to control what records are in that found set before the delete step deletes them.
I suggest that for DoorPreps, you add some code that compares Get ( FoundCount ) to Get ( TotalRecords) before deleting all records. If the results of those two functions are equal, your script is about to delete all records from the table. Thus, you can set up an IF step that checks for this and that then halts the script so that you can inspect the records shown to see if you can figure out why your Find failed to find records.
The Copy Preps is a table that hold the records temporarily until they can be imported later in the script. So my first step is to go to that layout and delete all the records. That way the records I import there will be the only ones in that table. Once the script is done I don't need that data anymore.
The first find is for Door Preps. It finds all records related to the current record in Heading 4 and Deletes All those records making way for the new records I want to import.
Next I Find the Door Prep records I want to copy ($From) from and import them into Copy Preps.
Then I run a loop on those records (in Copy Preps) to set the Heading field to match the original related record.
Thin I import them back into Door Preps.
Like I said, it usually works slick. But when It doesn't, records are deleted in Heading 4!! Why there? And why so infrequently? As far as I can tell, Heading 4 layout shouldn't even be the active window during the Delete commands.
So my first step is to go to that layout and delete all the records.
But it may not delete all the records. I recommend a show all records step to be sure.
The first find is for Door Preps. It finds all records related to the current record in Heading 4...
Yes but exactly HOW does it do that? What citeria are you using?
... and Deletes All those records making way for the new records I want to import.
Yes, but for some reason you are either getting invalid search criteria on occasion or your find criteria is finding all records. I have suggested a way to detect this condition and halt the script so that you can investigate further. I suggest that you use a show custom dialog step that displays the values of any variables used with your stored find criteria when this situation is detected.
Thanks, Phil. I have added the "Show All Records" step you suggested.
The find criteria is DoorPreps::ProjectNo [==$$Proj} AND DoorPreps::Heading: [$To]
I can (and will) add the steps for Door Preps that will check the record count against the found count, but these are not the records I'm accidently deleting. The records are being deleted from the original layout Heading 4 table - not all of them, but the current found set. Is it possible that one of the Go To Layout steps is somehow being missed or corrupted so that the subsequent Delete All Records is happening on my original layout?
Or could it be that something other than just this script is deleting your records?
What you describe is typical of a script that uses Go to Related records step to bring up the records and delete them. If there are no related records and you don't check for that possibility in your script, the script fails to change layouts and the record deleting happens on the wrong table/layout.
But you don't use that script step here.
Another possibility is that you have a "delete" option specified in one of your relationships that you shouldn't have. With that option specified, deleting records in table A can delete records in Table B if they are related to the records so deleted. That also seems unlikely or I'd expect to see more records going missing on you every time you run the script, but maybe there's an additional detail that only sometimes leaves such relationship in place and thus only sometimes records disappear from Header.