unable to use if else in Expression rule

a!localVariables(
  
  local!reassigndetail: a!queryEntity(
    entity: cons!MPPCONSDSE_MPPTABSAM_task,
    query: a!query(
      selection: a!querySelection(
        columns: {
          a!queryColumn(
            field: "ReassignEmail"
          ),
          a!queryColumn(
            field: "samaccid"
          )
        }
      ),
      logicalExpression: a!queryLogicalExpression(
        operator: "AND",
        filters: {
          a!queryFilter(
            field: "Reassign",
            operator: "=",
            value: "Yes"
          ),
          a!queryFilter(
            field: "annoucncementid",
            operator: "=",
            value: ri!Announcementid
          )
        },
        ignoreFiltersWithEmptyValues: true
      ),
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: -1
      )
    ),
    fetchTotalCount: false
  ).data,

  
  local!fabdetails:
  a!forEach(
    items:local!reassigndetail,
    expression:rule!MPPER_getFabsnamefromsamaccid(fv!item.samaccid)

  ),
  local!fabdetails2: a!forEach(a!flatten( local!fabdetails).fabname,fv!item),
  local!samemails: a!forEach(a!flatten( local!reassigndetail).ReassignEmail,fv!item)
  ,

  
  local!samdata : a!queryEntity(
    entity: cons!MPPCONSCDT_SAMAccountability,
    query: a!query(
      logicalExpression: a!queryLogicalExpression(
        operator: "AND",
        filters: {
          a!queryFilter(
            field: "samemail",
            operator: "in",
            value:local!samemails
          ),
          a!queryFilter(
            field: "fabname",
            operator: "in",
            value:local!fabdetails2
          )
        },
        ignoreFiltersWithEmptyValues: true
      ),
      pagingInfo: a!pagingInfo(
        startIndex: 1,
        batchSize: 50
      )
    ),
    fetchTotalCount: false
  ).data,
  
  local!result:
  a!forEach(
    items:local!samdata,
    expression: cast(typeof('type!{urn:com:appian:types}MPPSAMAccoumtability'()),fv!item)
  ),
  local!result

  
  
)

this works fine if local!reassigndetail finds some data from DB using a!queryentity but when it doesn't return any data from db then it gives following error

Expression evaluation error at function a!forEach [line 47]: Invalid index: Cannot index property ’’fabname’’ of type String into type List of Variant

Please help

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Your code is unnecessarily difficult to read when pasted straight into your post as plaintext as all formatting and indentation is lost.  I would encourage you to edit your post and insert it instead into a Code Box ("Insert" --> "Insert Code" in the editor menu) so it's actually manageable.

  • 0
    Certified Lead Developer

    When the database returns 0 rows, it takes the form of "{}", which has the type "List of Variant".  You need to return at least one row with data in it before Appian can be aware of what the fields are even supposed to be.  You don't even get the structure without returning at least a row of data.

    So, the solution is to wrap your logic inside an if function, which contains three parameters:

    The first parameter uses APN_isEmpty() or length() = 0 to determine if you returned 0 rows,

    The second parameter returns whatever you want to return in the case of 0 rows of data being returned by the query, could be as simple as "{}",

    The third parameter is what you were doing before, which only works when you have data to work on.

    Over time, you'll learn to wrap practically everything you write in "if is empty" or "if is blank".  A great many of us call it "null safety."

  • 0
    Certified Lead Developer
    in reply to arshbirs0001

    That's better - though it might be good to copy the code out of Appian again since the code you posted still doesn't have indentation (making it still pretty hard to read).

  • a!localVariables(
      
      local!reassigndetail: a!queryEntity(
        entity: cons!MPPCONSDSE_MPPTABSAM_task,
        query: a!query(
          selection: a!querySelection(
            columns: {
              a!queryColumn(
                field: "ReassignEmail"
              ),
              a!queryColumn(
                field: "samaccid"
              )
            }
          ),
          logicalExpression: a!queryLogicalExpression(
            operator: "AND",
            filters: {
              a!queryFilter(
                field: "Reassign",
                operator: "=",
                value: "Yes"
              ),
              a!queryFilter(
                field: "annoucncementid",
                operator: "=",
                value: ri!Announcementid
              )
            },
            ignoreFiltersWithEmptyValues: true
          ),
          pagingInfo: a!pagingInfo(
            startIndex: 1,
            batchSize: -1
          )
        ),
        fetchTotalCount: false
      ).data,
      local!result:null,
      local!fabdetails,local!fabdetails2,local!samdata,local!samemails,
      if(length(local!reassigndetail)==0,
        local!result,
        
        {
          local!fabdetails:
          a!forEach(
            items:local!reassigndetail,
            expression:rule!MPPER_getFabsnamefromsamaccid(fv!item.samaccid)
    
          ),
          local!fabdetails2: a!forEach(a!flatten( local!fabdetails).fabname,fv!item),
          local!samemails: a!forEach(a!flatten( local!reassigndetail).ReassignEmail,fv!item)
          ,
    
    
          local!samdata : a!queryEntity(
            entity: cons!MPPCONSCDT_SAMAccountability,
            query: a!query(
              logicalExpression: a!queryLogicalExpression(
                operator: "AND",
                filters: {
                  a!queryFilter(
                    field: "samemail",
                    operator: "in",
                    value:local!samemails
                  ),
                  a!queryFilter(
                    field: "fabname",
                    operator: "in",
                    value:local!fabdetails2
                  )
                },
                ignoreFiltersWithEmptyValues: true
              ),
              pagingInfo: a!pagingInfo(
                startIndex: 1,
                batchSize: 50
              )
            ),
            fetchTotalCount: false
          ).data,
    
          local!result:
          a!forEach(
            items:local!samdata,
            expression: cast(typeof('type!{urn:com:appian:types}MPPSAMAccoumtability'()),fv!item)
          ),
          local!result
          
          
        }
        
      
      )
     
    
      
      
    )
    

    When I use if else,

    when 0 rows are return it shows 'null', when more are returned then below error:

  • 0
    Certified Lead Developer
    in reply to arshbirs0001

    What is the purpose behind calling a!flatten here?

  • 0
    Certified Lead Developer
    in reply to arshbirs0001

    Also what is the output of rule!MPPER_getFabsnamefromsamaccid()?

  • 0
    Certified Lead Developer
    in reply to Dave Lewis

    I see what you're doing.

    You're doing a!forEach(

    items: a!flatten(list).property,

    expression: fv!item

    )

    There's absolutely 0 point to using a!forEach.  The flatten function already takes a list.  It flattens a nested list into a single list, destroying the more complex structure.  The expression only returns the items.  Well, if a!flatten(list).property worked, you'd already have a list of results without even using a!forEach.

    My advice, make a separate rule that only goes up to that point then returns nothing but a!flatten(your list) and see what that does.  Then, see if you can actually use the dot notation to get the property you want out of it.  Then, see if you need a!forEach.  I don't think you will.

  • 0
    Certified Lead Developer
    in reply to Dave Lewis

    Also, please for your own sake always explicitly NAME items: and expression: parameters of a!forEach.  That will save your own head quite a lot of aching.