Skip to main content
June 15, 2022
Solved

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

  • June 15, 2022
  • 5 replies
  • 0 views

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
)
)

)

    Best answer by mikes0011

    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).

    5 replies

    mikes0011
    mikes0011Answer
    Brainy
    June 15, 2022

    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
    June 16, 2022

    Thank you Mike. Your suggestion was helpful.

    mikes0011
    Brainy
    June 21, 2022

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

    The Expression Guru
    csteward
    June 15, 2022

    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)
                  }
                )
              )
            }
          )
        }
      )
    )

    June 16, 2022

    Thank you Chris. Your suggestion was helpful.