I'm working on an expression rule that takes two inputs: an integer value (f

I'm working on an expression rule that takes two inputs: an integer value (fieldname is key), and a CDT array (AnyType) where one of the fields I want to search on is named Key..so MyCDT.Key. The expression rule is actually more complex then this but to isolate my issue I am giving only the specifics here. Anyhow, here is the rule:

= load(
wherecontains(
ri!key,
ri!MyCDT.Key
)
)

I'm getting the following error:
"There was an error testing the rule:
Expression evaluation error at function 'wherecontains' parameter 2 [line 2]:"

I'm pretty certain this has to do with the fact that something is lost in passing something over to the expression rule--that it has no clue what this AnyType is. Perhaps I need to do an explicit cast of my AnyType into a CDT array?

OriginalPostID-140688

OriginalPostID-140688

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer
    Hi Robbier, how are you trying to test this rule? It seems like you may be trying to directly test your load(wherecontains(...)) rule from the same rule interface, in which case you would receive the error message you're receiving. Since this rule has an Any Type parameter as an input, I'd try to invoke your rule from another rule. In addition, I would avoid using the load here if possible, can you use a with instead?

    Something like this worked for me. Hope this helps.
    RuleA:
    =with(
    myResult: wherecontains(
    ri!key,
    tointeger(ri!myCDT.Key)
    ),
    myResult
    )

    RuleB:
    rule!RuleA(2, {{Key: 1}, {Key: 2}, {Key: 3}})
  • You could either set ri!key to text and then use
    whereContains(ri!key, touniformstring(ri!myCDT.key))

    or use something like the below to explicitly cast the any type array to an array of type myCDT

    with(
    local!myResolvedCDT: apply(rule!resolveCDT(_), ri!myCDT),
    local!result: whereContains(ri!key, local!myResolvedCDT.key),
    local!result
    )
    where resolveCDT is
    'type!{nameSpace}myCDT'(
    key: ri!myCDT.key,
    field2: ri!myCDT.field2
    ......
    )
  • I believe your issue stems from the fact that you are passing in an array of CDTs; therefore, ri!myCDT.key is one level above the value for which you are actually looking. Try indexing one more level down, like this:

    fn!wherecontains(
    ri!key,
    fn!apply(
    fn!index(_, "key", ""),
    ri!myCDT
    )
    )
  • Thanks to all for your help. Each of the techniques and advice offered gave me some new insights. There were a few issues going on as I found out, including the wherecontains having trouble dealing with empty and null arrays. Once I handled those cases the rule now seems to work.
  • Is this issue resolved? I am having same issue, can you reply?
  • The big issue I was having was that I was passing in my CDT array from a rule that I had set up for testing. I was creating the array MyCDT using a data dictionary method (e.g. {{Key:1, Value: "A"},{Key: 2. Value: "B"}, etc etc}. This was some sort of casting issue I was having. When I started testing this rule through a SAIL form (and using a the true CDT array type as ultimately passed in from my process) this problem went away. The other thing I was having a problem with was when my array was either null or zero length. Those two edge conditions were problematic for me, so I needed to test the CDT array against them by using:

    if(rule!lengthNullSafe(ri!MyCDT) > 0,
    wherecontains(ri!Key,ri!MyCDT.Key),
    0
    )

    The rule, rule!lengthNullSafe, is a simple null safe version of the length function where the array length gets returned as 0 if the array is null. Here is the rule that we created:

    if (isnull(ri!input), 0, length(ri!input))

    Pretty simple but by making those adjustments I was able to now test for both 0 and null in the array.