Are you using FileMaker 11? I will assume so, but this method only works in 11. (A slightly different approach can be used for FileMaker 10 and older.)
Add an AddressID serial number in your Addresses table if you do not already have one. Use Replace Field contents to update existing addresses with this serial number and also to update the next serial value setting at the same time.
Define a portal filter expression on the upper portal:
$$SelectedAddress = Addresses::AddressID or IsEmpty ($$SelectedAddress)
Make your "portal list" a second portal, but without the filter.
Use Button setup... to turn your calculation field into a button that runs this script:
Set Variable [$$SelectedAddress ; Value: Addresses::AddressID]
Refresh Window [Flush Cached Join Results]
Now, when you click a row in the lower portal, the upper portal will update to display that address. You can also use conditional formatting to change the fill color of this calculation field in the lower portal to show which address line is currently selected if you want.
I'd also write this script:
Set Variable [$$SelectedAddress; value: ""]
and use onRecordLoad to perform it so that each time you bring up a contact record, it defaults to the first record in your portal. If you sort your portal by IsDefault field in descending order, this triggered script will start you out with the default address each time.
Thanks Phil! I will try and get this working. And yes I am using FileMaker 11.
Okay, everything seems to be working. Couple of questions:
- What does Refresh Window [Flush Cached Join Results] do?
- The onRecordLoad you're referring to is in Layout Setup --> Script Triggers, or the portal Script Triggers?
- What script do I have to write for that "Save" button in order to commit that record and create a new record (Note: I only want the record to commit if the Save button is clicked, not merely by creation of a new record)?
1) It forces the filtered portal to update, so you need this to take place any time you change a value referenced in the portal's filter expression
2) Layout setup.
3) That would be difficult to do from a portal. It's just very easy to commit and save a portal record just by clicking on another part of the layout.
You might need to define a set of global fields for adding addresses and then use scripts to move the data into a new related record (or update an existing record) when save is clicked. This would change the script to bring up an address--in fact you wouldn't need a portal at all for this in the top section as you'd now be using a series of Set Field steps to move address data in and out of your global fields.