Skip to main content
September 2, 2021
Question

Search a list of Dictionaries

  • September 2, 2021
  • 2 replies
  • 0 views

Hello,

Is there a way in appian where I can search a list of dictionaries for any item in that list that meets a specific set of properties

For example, say I have the following list:

{{storeNum: "003", taskCode: "A", numOfTasks: 1292},

{storeNum: "003", taskCode: "B", numOfTasks: 5},

{storeNum: "003", taskCode: "C", numOfTasks: 8},

{storeNum: "003", taskCode: "D", numOfTasks: 5},

{storeNum: "004", taskCode: "A", numOfTasks: 208},

{storeNum: "004", taskCode: "C", numOfTasks: 2}} 

Now, I want a function, that when I feed it a storeNum and taskCode, to return to me the numOfTasks if it exists in the list, and returns 0 (or any indication) when no dictionary items with the specified combination of storeNum and taskCode exists in the list. 

for example, if I call function(storeNum: "003", taskCode:"A") it will return 1292

However, if I call function(storeNum: "004", taskCode:"B") it will return 0

Is there something like this, or similar? I am struggling to wrap my head around how list comprehension works in appian. 

thank you,

    2 replies

    September 2, 2021

    You can achieve this in Appian by creating an expression rule. Your rule will have inputs as the storeNum and taskCode. 

    Please go through the below links which will help you out - 

    https://docs.appian.com/suite/help/21.3/Expression_Rules.html

    https://docs.appian.com/suite/help/21.3/Appian_Functions.html

    A sample format would be something like this. 

    a!localVariables(
      local!data: {
        a!map(
          storeNum: "003",
          taskCode: "A",
          numOfTasks: 1292
        ),
        a!map(
          storeNum: "003",
          taskCode: "B",
          numOfTasks: 5
        ),
        a!map(
          storeNum: "003",
          taskCode: "C",
          numOfTasks: 8
        ),
        a!map(
          storeNum: "004",
          taskCode: "A",
          numOfTasks: 208
        ),
        a!map(
          storeNum: "004",
          taskCode: "C",
          numOfTasks: 2
        )
      },
      local!indexToQuery: intersection(
        wherecontains(ri!storeNum, local!data.storeNum),
        wherecontains(ri!taskCode, local!data.taskCode)
      ),
      if(
        count(local!indexToQuery) = 0,
        0,
        index(
          local!data.numOfTasks,
          local!indexToQuery
        )
      )
    )

    September 2, 2021

    Thank you sir, this is exactly what I needed. The use of the intersection function is so elegant.