scripts:catalogs

Catalogs

You can get values from catalogs and paste them into forms using the getCatalog method. For example, you can calculate table values or filter a catalog with values taken from another one.

Suppose a user selected a state in the State field and we only want cities from the selected state to be available in the City field.

In our example, the State field corresponds with the States catalog while the City field corresponds to the Cities catalog. There is also a States and cities catalog that lists cities by region.

To filter the list of cities by selected state, add the following script to the form:

let catalogItems = null;

form.getCatalog("USA states and cities").then(items => {
  catalogItems = items;
});

form.onChange(["State"]).setFilter("City", state => {
  const [region] = state.changes;

  if (!catalogItems || !region || !region.columns)
    return null;

  const regionCol = state.changes[0].columns["State"];
  const filtered = catalogItems
    .filter(item => item.columns["State"] === regionCol)
    .map(item => item.columns["City"]);
  return filtered.length > 0
    ? {
        values: filtered
      }
    : null
});

Pay attention: you can filter catalogs not only by their name, but also by ID number. In this case, the code in the script will look like this:

let catalogItems = null;

form.getCatalog(123).then(items => {
  catalogItems = items;
});

Was this article helpful?

Yes, thanks! No, I have a question