Skip to main content
June 29, 2022
Solved

How to update a single column value of multiple rows in an array with a particular value in process model?

  • June 29, 2022
  • 5 replies
  • 1 view

Use Case : I will be uploading multiple documents for a single question - Document to questions (many to one relationship )

I have a CDT for Question and another CDT for documents.(Questions and documents are two different tables in database.) Since, I need to populate the document id in the document table based on the questionaries entry - For now, I am getting the primary key of question table first (after making an entry in the database)and then  trying to pass the value to document table for multiple rows.

I am making an entry in the Question table via the process model using write to data store entity smart service, then using a script task I get the ID (Primary Key) of the newly inserted Questions and save it against a new process variable. Then, there is another write to data store entity smart service and in the input node I have mapped the newly created Questions ID against the particular column. When there are multiple attachments, only the first row gets updated with newly created Question ID in the respective column and all other rows are updated with null value.

In short - Multiple value update is not working - Is there a way to update the newly created Questions ID against a particular column for all the rows present in array? Please elucidate.

Best answer by peter.lewis

You should use a!forEach() and a!update() to iterate over the list of rows and update the corresponding field in your script task. Here's an expression snippet as an example: 

a!localVariables(
  local!data: {
    a!map(id: 1, foreignKey: null, name: "Test One"),
    a!map(id: 2, foreignKey: null, name: "Test Two"),
    a!map(id: 3, foreignKey: null, name: "Test Three"),
  },
  a!forEach(
    items: local!data,
    expression: a!update(
      fv!item,
      "foreignKey",
      123
    )
  )
)

5 replies

peter.lewis
Employee
June 29, 2022

You should use a!forEach() and a!update() to iterate over the list of rows and update the corresponding field in your script task. Here's an expression snippet as an example: 

a!localVariables(
  local!data: {
    a!map(id: 1, foreignKey: null, name: "Test One"),
    a!map(id: 2, foreignKey: null, name: "Test Two"),
    a!map(id: 3, foreignKey: null, name: "Test Three"),
  },
  a!forEach(
    items: local!data,
    expression: a!update(
      fv!item,
      "foreignKey",
      123
    )
  )
)

June 30, 2022

[mention:6b9f80ff18f9420082d13ef2d5121d56:e9ed411860ed4f2ba0265705b8793d05] Thank you. It worked.

csteward
June 29, 2022

As there are a few ways to do this, one I regularly use is a script task output to update that specific ID value in your CDT via fn!repeat(), which creates a list of your IDs.

fn!repeat(count(pv!YOUR_CDT),pv!ID_VALUE)

Save this into the field you would like to update in the Questions CDT.

June 30, 2022

[mention:23a2ab7d11d24e69bfaa8e50e14d1914:e9ed411860ed4f2ba0265705b8793d05]Thank you for your reply. I tried using fn!repeat() and I got the below error.

Expression evaluation error at function fn!repeat: Cannot repeat list type

So, I tried a!forEach() as mentioned by Peter and it worked for me.

csteward
June 30, 2022

Glad you have a solution!  To note on fn!repeat(), if the single value you are looking to repeat is stored in an array for some reason, you will want to convert it to a single value such as:

fn!repeat(count(pv!YOUR_CDT),index(pv!ID_VALUE,1,null))