Hi All,
I want to reject values in a CDT array on the basis of a condition.
For ex - I have a CDT array of type, suppose, type!someType(a,b), where a and b are fields in CDT. Now, My array looks like below:
{
type!someType(a: 1, b: null)
type!someType(a: null, b: 1)
type!someType(a: null, b: null)
},
I want to filter this array and get only those CDT values where a <> null.
What can be the possible way to do it?
Discussion posts and replies are publicly visible
Try something like this. Note in the wherecontains() you may have to change the apply(fn!tointeger..) to match your data type, as wherecontains() is type-sensitive.
a!localVariables( local!data: { {a: 1, b: null}, {a: null, b: null}, {a: null, b: 1}, {a: 1, b: 2}, {a: 2, b: null} }, index( local!data, wherecontains( reject( fn!isnull, local!data.a ), apply(fn!tointeger,local!data.a) ) ) )
a!forEach() handles this more gracefully, IMHO, and allows us to skip the awful deprecated "apply()" function.
(If I may borrow your original code..)
a!localVariables( local!data: { {a: 1, b: null}, {a: null, b: null}, {a: null, b: 1}, {a: 1, b: 2}, {a: 2, b: null} }, a!forEach( items: local!data, expression: if( isnull(fv!item.a), {}, fv!item ) ) /*index(*/ /*local!data,*/ /*wherecontains(*/ /*reject(*/ /*fn!isnull,*/ /*local!data.a*/ /*),*/ /*apply(fn!tointeger,local!data.a)*/ /*)*/ /*)*/ )
You may! I agree with the a!forEach() replacement. Still kicking the bad apply() habit.. Heck, we still have doForEach in a few older Portal PMs circa 2010 ;)
wow/ouch ;-)
May I indicate one issue with the foreach-filter habit. In case no item matches the condition, the created list is a list of empty lists {{}, {}, {}, ...}. This can break any subsequent code and can be a pain to debug. This is why I still recommend the "old" looping functions filter() and reject() and asked Appian to come up with new filter functions similar to a!foreach().
ChrisMike SchmittStefan Helzle
Thanks everyone for the elaborate responses. The solution provided worked.
FYI, I've noticed this before as well, and more recently I've figured out that wrapping the entire a!forEach() statement in a!flatten() also handles this scenario (with much less bad taste in the back of my mouth than using the old looping functions).
(In fact, I was considering including it in my answer above, but I thought for the moment it might be better to not over-complicate things.. now i'm not so sure)