What is your Context when this script executes? From your post, referring to a portal and the mention of a "related field" in your post's title both imply that you have at least two tables linked in a relationship.
At the time this script executes are you on a layout based on Contacts or a layout based on some other table such as a table of invoices?
That's correct yes, I've a second table. The context is that each customer can have several contacts (their own staff members if you will). The tables involved are Customers and Contacts.
When I execute the script, I'm on the Customers layout (which to be fair may be the issue) BUT I was kind of hoping, from a user experience point of view, that from within the Customers layout I could use a popover to add related data, such as their own staff members, as opposed to transferring the user to another separate view.
When I execute the script, I'm on the Customers layout
Which means that the new record step in the script that you posted is creating a new customer record and not a new contact record.
each customer can have several contacts
By what means are you linking a single customer to multiple contacts? Can the same contact be linked to more than one customer?
I was kind of hoping, from a user experience point of view, that from within the Customers layout I could use a popover to add related data, such as their own staff members, as opposed to transferring the user to another separate view.
No need for the user to change views. The script can change views, add the new record and then change back. The user need never see the view change.
If contact records are only linked to a single customer but customers can link to many contacts, you should have this relationship:
Customers::__pkCustomerID = Contacts::_fkCustomerID (though you may name things differently)
A script performed from a layout based on Customers might add a new Contact record like this:
Set Variable [$CustID ; value: Customers::__pkCustomerID ]
Go to Layout ["Contacts" (Contacts)]
Set FIeld [Contacts::_fkCustomerID ; $CustID ]
#Put your set field steps to transfer data from the global fields to corresponding fields in Contacts here
Go to Layout [origional layout ]
PS. this script will, as a side affect close any open popover. If you want to keep the popover open when this script is performed, give the popover an object name via the name box in the inspector and include a go to object step that specifies that object name as the final step of the above script.
Second note, a change in layouts by this type of script can trip all kinds of script triggers. There are ways to use a global variable to keep those scripts from doing anything that might interfere with the execution of this script.
I've added a couple of rows based on the script as I've got a many to many relationship between customers and their contacts, but it was pretty straightforward given the content of the original script.
I hope to be able to use that logic elsewhere as well, so thanks for your help and patience!
Here's a bit more that you may find useful:
The key issue that this type of script can "trip" is that nearly every script trigger on two different layouts might get tripped when the script changes to a different layout and then changes back. If you make a habit of writing script trigger performed scripts with this type of code:
If [ Not $$TriggersOff ]
Put rest of script here
You can then "disable" script triggers by using:
Set Variable [$$TriggersOff ; value: True ]
Before changing layouts and then use
Set Variable [$$TriggersOff ; Value: False ]
To re-enable them before the script exits or halts.