Create New Items Using a Custom Picker

In this post, I will show you how to develop a picker field that also allows you to create new items on the fly.

I assume that you already know about the picker field components in Appian. Currently, Appian provides picker fields to select users and/or groups, documents and/or folders, and records. These have a predefined behaviour and are simple to use.

Then there is the custom picker field. This one is more tricky to use, as it requires a separate expression to look up items matching the entered text. Please visit the Appian documentation first to understand the basic principles. I will then explain, how to use a certain feature of the DataSubSet data type to make the magic work.

This is a basic Appian interface. The rule input items is of type List of Map.

a!pickerFieldCustom(
  label: "Custom Picker",
  value: ri!items,
  saveInto: ri!items,
  selectedLabels: index(ri!items, "name", null),
  suggestFunction: rule!SSH_PickerFieldSuggestFunction(
    search: _
  )
)

Then there is the required suggest function. The rule input search is of type Text.

a!localVariables(
  local!items: {
    a!map(id: 1, name: "Sony"),
    a!map(id: 2, name: "Panasonic"),
    a!map(id: 3, name: "Metacortex")
  },
  a!dataSubset(
    startIndex: 1,
    batchSize: count(local!items) + 1,
    sort: null,
    totalCount: count(local!items) + 1,
    data: append(
      local!items.name,
      ri!search
    ),
    identifiers: append(
      local!items,
      a!map(
        name: ri!search
      )
    )
  )
)

In this suggest function, I define a list of items, which you would then replace by some query to fetch items matching the search. The documentation, I linked above, recommends to only put the primary key values into the identifiers field of the created data subset. But, when you look at the data type of that field, you will notice that it is of the Any type. This means that it can hold a list of any data type.

To allow the user to add a new item, I append that new item to the created data subset. Take a close look at the parameters data and identifiers.

Pay attention to what you append to the identifiers field. This is at the edge of what Appian had in mind when building this. I was not successful directly using record type data. Casting it into a list of maps works perfectly.

In the process model, you will need some logic, like a null-check on the primary key field, to identify any newly created item and write them to the database.

Summary

When I was building this a while ago, and I understood that the field identifiers in the data subset is of the Any type, the solution was obvious. But that took me a while. Instead of passing only the primary key of the selected object, I then pass the list of full objects.

With this little new insight, I was able to offer my customer a great solution to their needs and an excellent UX users will enjoy.

I hope, this inspires you to look closer and find all these great little gems in Appian.

One thought on “Create New Items Using a Custom Picker

Leave a Reply