Parsware
All articles

How-tos

How to add a lookup that points at several tables

One Customer field that holds either an account or a contact: choose the tables, pick one of each on two records, and find them from the Related tab. How-to video H021, step by step.

A bright cover reading "How-to · H021: Add a lookup that points at several tables", with "One field, an account or a contact" under it, the steps Several tables → Relationships → Form → Pick a table, and two screens from the video.

How-to H021 · 2 min 18 s. A complete tutorial: every click and value the video uses, the two relationships the field creates, and how its value looks to a script. Every picture is a frame of the video.

Sometimes a customer is a company and sometimes a person. A multi-table lookup is one field that holds either. In this tutorial you add a Customer field to Opportunity that points at an account or a contact, fill it in both ways, and find the opportunity again from the account.

Before you start

  • The Opportunity table with the Account lookup, and Accounts in the Sales Tracker app, from How to add a lookup to another table.
  • Two opportunities, Contoso fleet renewal and Northwind renewal.
  • An account, Contoso Group, and a contact, Avery Kim. Use any account and contact you have.

Lookup or lookup (several tables)?

Lookup Lookup (several tables)
Points at One table Two or more
Stores The record's id The record's id, and which table it is in
Foreign key Yes No: one column can't reference several tables
Relationships One One per table
Use it for An opportunity's account A customer that is an account or a contact

Use the plain lookup whenever one table will do. Use this one when the answer genuinely can be either.

Part 1: Add the field

  1. Open Solutions, then Sales Tracker, select Tables and open Opportunity. The field goes here, because each opportunity has one customer.

  2. Select New. Type the display name Customer. The logical name fills in as tsp_customer.

  3. Set Data type to Lookup (several tables).

  4. Open Related tables:

    1. Type acc and pick Account.
    2. Type cont and pick Contact.

    Pick the commonest table first. The record picker opens on it.

  5. Read the note: saving creates one relationship for each table, and the tables are fixed after that.

The New field panel: Customer, data type Lookup (several tables), related tables Account and Contact, with a note that saving creates one relationship for each table.

  1. Select Save. The type reads Lookup (several tables) → account, contact.
  2. Select Relationships. There are two new ones, one for each table, named from the table and the field (the second ends in _2).

Part 2: Put it on the form

  1. Select Forms and open Information.
  2. Drag Customer in, under Account, and select Save.

Part 3: A company as the customer

  1. Open Sales Test's address, open Sales Tracker and open Contoso fleet renewal.
  2. Select the Customer box. The tables come first, and Account is selected.
  3. Type Contoso and pick Contoso Group.
  4. Select Save & Close. It goes back to the list of opportunities.

Part 4: A person as the customer

  1. Open Northwind renewal. This customer is a person.
  2. Select the Customer box, then Contact.
  3. Type Avery and pick Avery Kim. The icon beside the name says it is a contact.
  4. Select Save & Close.

An opportunity with the Customer box open, offering Account and Contact as tables to search, with Contact chosen.

Both opportunities now have a customer: a company, and a person.

Part 5: See it from the account

Each table has its own relationship, so an account lists the opportunities whose customer it is.

  1. Select Accounts and open Contoso Group.
  2. Select Related. Opportunity now has two lookups to Account, so each list is named by its field: Opportunities (Account) and Opportunities (Customer).
  3. Select Opportunities (Customer). Contoso fleet renewal is listed.

Avery Kim's contact record has the same kind of list for Northwind renewal, in any app that shows contacts.

The field as text

The field lists its tables, in the order the picker offers them:

{
  "logicalName": "tsp_customer",
  "displayName": "Customer",
  "dataType": "PolymorphicLookup",
  "targetEntities": ["account", "contact"]
}

In the database it is two columns: tsp_customer holds the record's id, and tsp_customer_type holds which table it is in, by logical name. Everywhere else, a script, an API, an import, it travels as one value, the table and the id joined by a colon:

account:3f2b6c1e-…
contact:9a41d07c-…

So a script can tell which kind of customer it has:

var opp = getRecord("tsp_opportunity", getRecordRef("record"));
if (opp.tsp_customer.includes("contact:")) {
  // a person
} else {
  // a company
}

If something goes wrong

  • Only one table can be picked. The data type is Lookup, not Lookup (several tables).
  • You need a third table later. The tables are fixed after saving, because rows already hold references checked against them. Delete the field and add a new one under a different logical name. Deleting a field keeps its stored columns, so a field added back under the same name would come back filled in.
  • A linked record was deleted. There is no foreign key, so the reference stays and reads as unresolved. Pick another customer.

What you built

One Customer field that holds an account or a contact, with a related list on both tables. Each table has its own relationship, so everything that works for a plain lookup works for this one.

Read more

Is there a task you would like to see done step by step? Tell us. parsware.com/contact