Skip to main content
New Participant
Jun 15, 2022 2:59 PM UTC
Solved

Is it possible to filter Dataquest that is created using a!foreach?

Hello,

I've created a datasubset using a!foreach. Now want to filter the data on the datasubset depending on the value for one field. Can you please suggest on this?

a high level snippet is below. Below rule returns a datasubset & on that datasubset I am trying add a filter. 

todatasubset(

a!forEach(
items: rule!Irulename(),
expression: 'type!{urn:com:appian:types:cdt}|cdt_name'(
fields:,
completed: rule!newRuleToupdateafield
)
)

)

    mikes0011
    mikes0011Answer
    Brainy
    Jun 15, 2022 4:55 PM UTC

    I'm not sure what the requirements are of the "filtering" you want to perform, but in general, you can use a!forEach() to loop over items in any array (including dictionaries, CDTs, etc), and any items that don't match the condition you want, just return the empty set "{}". 

    Note: If there's a chance that all rows may be filtered out under any condition, you need to wrap the whole thing in "a!flatten()" as well, since otherwise using the above technique would return a list of empty sets specifically when all results are empty sets (otherwise it performs as expected).

    The Expression Guru

    5 replies

    mikes0011
    mikes0011Answer
    Brainy
    Jun 15, 2022 4:55 PM UTC

    I'm not sure what the requirements are of the "filtering" you want to perform, but in general, you can use a!forEach() to loop over items in any array (including dictionaries, CDTs, etc), and any items that don't match the condition you want, just return the empty set "{}". 

    Note: If there's a chance that all rows may be filtered out under any condition, you need to wrap the whole thing in "a!flatten()" as well, since otherwise using the above technique would return a list of empty sets specifically when all results are empty sets (otherwise it performs as expected).

    The Expression Guru
    New Participant
    Jun 16, 2022 7:02 AM UTC

    Thank you Mike. Your suggestion was helpful.

    mikes0011
    Brainy
    Jun 21, 2022 2:58 PM UTC

    Thanks!  I would also appreciate a "verify answer" click if you're satisfied.

    The Expression Guru
    csteward
    Brainy
    Jun 15, 2022 6:44 PM UTC

    As an example, try this in a blank interface:

    a!localVariables(
      local!options: {"A","B","C","D"},
      local!selected: null,
      local!data: todatasubset(
        a!flatten(
          fn!reject(
            fn!isnull,
            a!forEach(
            items: local!options,
            expression: if(
              fv!item=local!selected,
              null,
              {
                data: fv!item
              }
            )
          )
          )
        )
      ),
      a!columnsLayout(
        columns: {
          a!columnLayout(
            contents: {
              a!dropdownField(
                label: "Filter Out",
                choiceLabels: local!options,
                choiceValues: local!options,
                placeholder: "-- Select --",
                value: local!selected,
                saveInto: local!selected
              )
            }
          ),
          a!columnLayout(
            contents: {
              a!richTextDisplayField(
                label: "Result",
                value: a!forEach(
                  items: local!data,
                  expression: {
                    a!richTextItem(
                      text: fv!item.data
                    ),
                    char(13)
                  }
                )
              )
            }
          )
        }
      )
    )

    New Participant
    Jun 16, 2022 7:03 AM UTC

    Thank you Chris. Your suggestion was helpful.