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
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."
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:
What is the purpose behind calling a!flatten here?
Also what is the output of rule!MPPER_getFabsnamefromsamaccid()?
it returns list
List of what / what is the structure of the list returned? Because your subsequent code assumes that local!fabDetails is a dictionary containing a property named fabName.
List of dictionary .
Flatten takes : {{1,2,3},{4},{},{5,6,7,8},{{9},{10,11}},12} and outputs {1,2,3,4,5,6,7,8,9,10,11,12}. It makes a nested array of arrays into just an array. Do you even need that operation to be performed if you have a list of dictionary?
What does flatten() do to a list of dictionary? Would it similarly squash the internal structure so you couldn't see the individual fields anymore?
Unknown said:What does flatten() do to a list of dictionary?
AFAIK it just makes sure it's a flatt array of dictionary, instead of a list of nested lists. I believe it's being used redundantly (or at least, unnecessarily) in the above code sample.