Am I correct that these records are shown in a portal? (You make no mention of your portal, but your script is the classic script for adding related records to a table that are typically then shown in a portal.)
If so, you don't have two records. You have the new record plus the blank "add row" shown in a portal when the underlying relationship has the "allow creation of records via this relationship" enabled. This allows you to add records in the portal simply by entering data in a field of this blank "add row" and FileMaker copies over the needed match field values to link the new record to your current layout's record for you.
If you are always going to use the above script to add related records, you can clear the check box for this option and the "add row" will disappear from your portal.
I have not created a portal on the popover area. I have just placed a field object that is linked to the building table, and is displaying the building_name field. On the same popover area I have placed 2 buttons:
A button to cancel and a button to save.
When I press the 'save' button, the code I have screenshot on original posting is used.
When I check the building database records using a standard table layout - I can see that 2 records have been created, a record with the text that has been input into the building_name field on the popover area and a new blank record.
The script you show cannot create more than one record, but it adds it to the current found set on the layout where it creates it. Might there have already been a blank record there before this script was run? If you run the above script 3 times, what do you get? 1 blank record and 3 new ones or 6 new records, 3 of which are blank?.
The other thing to check is that scripts that change layouts also can trip a number of script triggers on either or both of the two layouts so there might be another script out there that is also being performed when you run the script that you've posted.
Here is the Layout view......
The layout view is a form view and called Expense Detail. It is associated to Expense period Table.
The fields within Expense Period Table are placed in the area labelled "1"
The area labelled "2" is a Portal and is associated with the Expenses Table". The Expenses and the Expense Period Table are related by a many to one relationship.
On the Portal is a field called "Building", this is populated from a drop down list which uses data from the Building Table. The field is called building_name.
What I am trying to do is create a new building record / name when it does not exist in the drop down list when populating the Building Name field in the Portal area.
I am doing this by adding the "Add Building Popover" button which displays the area Labelled "3"
Within this area I have added the building_name field from the Building Table and when the "Done" button is clicked it calls the script that I have posted earlier.
I have checked and it seems to be doing 2 things..... Creating a blank record and an actual record in the Building Table and it is also overwriting records??? It is not working correctly and I am sure it is because I am not using the objects correctly... I can't be far away but at the moment it feels it!
I am really new to Filemaker and I am not a coder, therefore please excuse my lack of knowledge.....
Your script does exactly one thing:
It creates a new blank record in the Building table. Nothing more. Nothing on the layout you have shown, except the value list on the building name field will change when you run this script. You'd have to check the total number of records on a layout based on the building table, then run this script and note what change occurs in the total number of records in that table.
If additional records are appearing in the building table, they aren't being added by this script. That's why I mentioned the possibility that the go to layout steps in the script might be tripping script triggers that perform another script that might affect the results that you see here.
The set field step that follows is missing the calculated result parameter needed in order to assign a value to the building name field.
From what I see in the script, it should read: Set Field [Building::Building_Name ; $building_Name ]
It won't overwrite any records. It can't. It doesn't even modify the fields of any records directly. The only way any data in this new record can be entered into fields of this new Building record is if the field has an auto-enter field option of one type or another.