Skip to main content
July 29, 2021
Solved

Process Model Cant Write a List to the DB

  • July 29, 2021
  • 6 replies
  • 0 views

My process model has an integration task that calls an API and then follows up to write into my database, but I keep getting this error when I run it:

Cannot index property 'property' of type Text into empty List of Dictionary) (Data Outputs)

From what I'm understanding is that the PM can't write a List ({}) to the DB and rightfully so. So in my expression for the output I have this:

if(
    length(
        rule!myIntegration().result.body.people.person.address
    ) =0,"",
    rule!myIntegration().result.body.people.person.address.state.value)

I try to tell the expression, that if the List ({}) is empty (or length of 0), then to make it an empty string ("") so it can write an empty string to the DB instead of a List (why the error happens), otherwise if not empty or not 0, to add the value to the DB. I would think this work but I keep getting this error. My JSON API string is as follow:

{    
    "people": [
        {
            "id": 1,
            "person": {
                "address": { 
                    "state": { "value": "VA" }
                }
            }
        },
        {
            "id": 2,
            "person": {
                "address": {}
            }
        }
    ]
}


As you can see, the second user is what I'm trying to take into account in the expression. I've tried it many different ways and for some reason I still get this error.

Best answer by madhusudans0001

Okay, got it. That's simple. I was not aware of your use case and intentionally put the reject() part. You can simply remove the "reject" function and you'll start getting the empty strings wherever the address is null.

Replace your expression with the below code:

if(
    length(rule!myIntegration().result.body.people.person.address) = 0,
    "",
    a!forEach(
        items: rule!myIntegration().result.body.people.person.address,
        expression: index(fv!item, "state", "value", "")
    )
)

Hope it helps!

6 replies

madhusudans0001
July 29, 2021

The issue that you are facing here is not related to the "process model unable to write a list to DB", although that's a different thing to discuss, here in your expression rule, you are trying to check if the length of the array of "address" is empty or not, if it is, then return an empty string, otherwise, return all the states' "value".

Now, if we look closely at the JSON that you provided, your "if" condition will go into the false block because out of two "state", you have one as non-null. But, at the same time, when you're trying to fetch the states' "value" in your false block, you are trying to fetch the data for the second user's state as well, which is null. Thus, you're getting this error "Expression evaluation error: Invalid index: Cannot index property 'state' of type Text into empty List of Dictionary".

To fix this, you can try replacing your expression with the following snippet:

if(
    length(rule!myIntegration().result.body.people.person.address) = 0,
    "",
    reject(
      fn!isnull,
      a!forEach(
        items: rule!myIntegration().result.body.people.person.address,
        expression: index(fv!item, "state", "value", null)
      )
    )
)

Hope it helps!

alexl0007Author
July 29, 2021

That seemed to make the error go away, but a side effect of that is that it captures the next users info into the one where its empty and then the rest of the users have the next users values.

madhusudans0001
July 30, 2021

I got a little confused. Could you please elaborate on your use case?