Lookups in Parsware: linking one record to another
A lookup field links a record to one in another table. Adding one also creates the relationship between the two tables, gives the form a search that finds records as you type, and puts a Related tab on the record at the other end.

A training course has a trainer, an invoice has a customer, an order line belongs to an order. Each one links a record to another record, and in Parsware that link is a lookup field.
A lookup stores the other record itself, not its name. So the link survives a rename, a person can't mistype it, and both ends know about it. The course shows its trainer, and the trainer's record lists every course they teach.
We carry on with the Training Course table from Every field type, explained, and its Trainer field. Every picture is the real product.
Add a lookup field
On the table's Fields page, choose New. Give the field a name, set Data type to Lookup, and choose the Related table: the table whose records it points at. Our Trainer points at Contact.
That's all there is to it. There's no separate step to link the two tables, because the lookup is the link.
The relationship comes with it
Saving the field also creates a relationship between the two tables, and a real foreign key in the database. Open the table's Relationships page and it's there, named after the table and the field:

Read each row as one record on the left to many on the right. One contact can train many courses, and each course has one trainer. That's why the lookup lives on the "many" side, on the course.
The two Booked By rows come from the lookup to several tables that article 005 added. It gets one relationship for each table it can point at, so there's one for accounts and one for contacts.
The two always go together. Delete the lookup field and its relationship goes with it, so nothing is left pointing at a field that no longer exists.
On the form: search, don't type
Put the field on the form and it becomes a search box. Type part of a name and the matching contacts appear, each with a second line so two people with similar names can be told apart:

Pick one and the field shows their name. Behind it, the course stores the contact's id, which nobody has to see or remember.
Which columns the second line shows, and which columns a search matches, are set on the related table. They get their own article later in this series.
At the other end: the Related tab
Now open the trainer. A contact's form has a Related menu after its tabs, listing every table whose records point at this one:

Training Courses appears twice because two lookups on that table point at contacts: Trainer and Booked By. When that happens, the menu adds the lookup's name in brackets so you can tell them apart. We didn't have to set any of this up. Adding the lookup was enough.
Choose Training Courses (con_trainer) and it opens as a tab beside the form's own, listing just the courses Riley teaches:

The Trainer column isn't shown, because every row would say Riley Miller. The tab has its own New, Edit and Refresh. Close it with the ×.
Add from the related tab
New on the related tab opens a new course with the link already filled in:

This is how you add a line to an order or a task to a project without leaving it. Save and close, and you're back on the trainer, on the same tab.
A related tab needs a saved record, because the related records point at it. On a record you're still creating, the menu tells you to save first.
The lookup as the table stores it
In the table's document, a lookup is a field like any other, with one extra setting: the table it points at.
{
"logicalName": "con_trainer",
"displayName": "Trainer",
"dataType": "Lookup",
"targetEntity": "contact"
}
That is the whole definition. The relationship, the foreign key, the search on the form and the Related tab on the contact are all worked out from these four lines, which is why there is nothing else to set up. When the table travels to Test in a solution, the import checks that the target environment has a contact table before it creates the column, and stops with a clear message if it doesn't.
Which side the lookup goes on
The rule is short: the lookup goes on the side that has one of the other. Each course has one trainer, so the course holds the lookup. Each order line belongs to one order, so the line holds it. You never put a list of courses on the contact; the contact's Related tab is that list, worked out from the courses that point at it.
A few designs people reach for, and what to do instead:
- "An order has many lines." Put a lookup to Order on the Order line table. The order's Related tab shows its lines, and New on that tab adds one with the order filled in.
- "A course has many trainers, and a trainer has many courses." That is many-to-many. Make a table in the middle, Course trainer, with a lookup to each. Relationships between tables shows it built.
- "A category belongs to a parent category." A lookup can point at its own table. That is how a tree is built, and Category trees shows the records nested inside each other.
- "A booking can be made by a company or a person." That is one lookup to two tables, the subject of the next article.
What the search finds, and what it shows
The search box on the form isn't fixed. Two settings on the related table decide it, and they apply to every lookup that points at that table:
- The lookup view decides the columns of Advanced lookup, the grid that opens from the bottom of the drop-down, where every column has room. The drop-down itself shows each record's name and its second line.
- The quick find view decides which fields your typing is matched against. If people search contacts by their company or their phone number as well as their name, add those fields to it.
Both are covered in Lookup views and quick find, and the picture and second line beside each record in A picture and a second line beside every record.
A lookup in a view
Add Trainer to a view of courses and the column shows each trainer's name, not an id. A filter on it offers equals, does not equal, is one of, is empty and is not empty, so you can ask for every course Riley Miller teaches, every course taught by one of three people, or every course that still has no trainer, which is often the most useful view of all.
This is the payoff of storing the record rather than its name. If Riley's name is corrected to Riley Millar, every course, view and report shows the new spelling at once, because there was only ever one copy of it.
Reading a lookup from a script
In BS Lang, a lookup's value is the id of the record it points
at. To use anything about that record, read it with getRecord. This server rule refuses a course
whose trainer has no email address, because the confirmation emails would have nowhere to go:
var trainerId = getValue('con_trainer');
if (!isEmpty(trainerId)) {
var trainer = getRecord('contact', trainerId);
if (isEmpty(trainer) || isEmpty(trainer.emailaddress1)) {
throwValidationError('The trainer needs an email address before they can be given a course.');
}
}
getRecord returns nothing when there is no such record, or when the person saving isn't allowed to
see it, so the script checks before using it. The other direction, every course this contact
teaches, is a query: queryRecords('con_trainingcourse', 'con_trainer', trainerId).
Query records from a script has both, with totals and
joins.
Deleting either end
Because a plain lookup has a real foreign key, the database protects the link.
- Delete a course and its trainer is untouched. The course pointed at the contact, not the other way round.
- Delete a trainer who still has courses and the delete is refused, with a message naming every table that still points at them. Nothing is emptied or deleted behind your back. Reassign the courses from the trainer's Related tab, then delete.
Relationships between tables, and what happens when you delete shows the message and the way through it, including the one case that behaves differently: a lookup to several tables, which has no foreign key.
Making a lookup required
A lookup can be required like any other field, on the field or on one form. A course with no trainer can't be saved from that form; the search box gets the red star and the message. Required, read-only and default controls explains why required is a form setting, and how to make it a guarantee with a server rule when an import or an integration must not leave it empty either.
Questions that come up
Can I change the related table later? Not on a saved field, for the same reason a field's type is fixed: every stored id belongs to the old table. Add a new lookup to the right table, fill it in, and remove the old field.
What if the record I need isn't there yet? Create it first, then come back to the lookup. A lookup can only point at a record that exists, which is exactly what keeps the link from being broken.
Can two lookups on one table point at the same table? Yes, as Trainer and Booked By both do. Each gets its own relationship, and the other table's Related menu lists both, named by the lookup. A purchase order with a Requested by and an Approved by contact is the usual example.
Does every record need a lookup to a person? No. Every record already has an owner, a user or a team, which says whose it is. Use a lookup for who a record is about, such as a trainer or a customer, and the owner for who is responsible for it. Change who owns a record explains the difference.
How many records can a lookup choose from? As many as the table holds. The search asks the database as you type, so you never scroll through a long list to find one.
In short
- A lookup field links a record to one in another table. Put it on the "many" side.
- Adding it creates the relationship and a foreign key. Deleting it removes both.
- On a form it's a search, and it stores the record rather than its name.
- The other table's records get a Related tab listing everything that points at them, with a New that fills in the link.
Next: one lookup that can point at several tables, such as a customer that is either an account or a contact.