Skip to main content
Known Participant
Aug 13, 2021 8:13 PM UTC
Solved

Update specific field on specific index

Hello All,

I just want to know how can I update a specific field to a specific index? example is I have 3 list of items, and I only want to update the 2nd index first name to "Jerry" and not "Henry". I tried to use updatearray() but its only updating the whole data or I am missing something? 

{

index: 1,

name: "John",

lastName: "Doe"

},


{

index: 2,

name: "Henry",

lastName: "Doe"

},


{

index: 3,

name: "Jenny",

lastName: "Doe"

},

Looking forward to anyone reply. Thank you!

csteward
cstewardAnswer
Brainy
Aug 13, 2021 9:04 PM UTC

As an alternative for prior versions of Appian, the Dictionary Manipulation plugin is very useful for these sorts of things:

a!localVariables(
  local!data: {
    { index: 1, name: "John", lastName: "Doe" },
    { index: 2, name: "Henry", lastName: "Doe" },
    { index: 3, name: "Jenny", lastName: "Doe" },
  },
  
  a!forEach(
    items: local!data,
    expression: if(
      fv!index=2,
      updatedictionary(
        fv!item,
        {
          name: "Jerry"
        }
      ),
      fv!item
    )
  )
)

7 replies

peter.lewis
Employee
Aug 13, 2021 8:55 PM UTC

Are you on 21.2? There's a great function that was recently released called a!update() that makes this pretty easy. The only tricky thing is that since you're updating a single value in a list of lists, you have to perform a!update() twice. Here's an example of how it works:

a!localVariables(
  local!data: {
    { index: 1, name: "John", lastName: "Doe" },
    { index: 2, name: "Henry", lastName: "Doe" },
    { index: 3, name: "Jenny", lastName: "Doe" },
  },
  a!update(
    data: local!data,
    index: 2,
    value: a!update(
      data: index(local!data, 2, {}),
      index: "name",
      value: "Jerry"
    ),
  )
)

Known Participant
Aug 14, 2021 2:01 AM UTC

Hello Peter, Thank you for your reply. I tried this but I can't update multiple data. ex. 2nd and 3rd index 

peter.lewis
Employee
Aug 16, 2021 12:53 PM UTC

You can still use a!update(), just use a!forEach() instead of the first update:

a!localVariables(
  local!data: {
    { index: 1, name: "John", lastName: "Doe" },
    { index: 2, name: "Henry", lastName: "Doe" },
    { index: 3, name: "Jenny", lastName: "Doe" },
  },
  a!forEach(
    items: local!data,
    expression: if(
      fv!index = 2,
      a!update(
        data: index(local!data, 2, {}),
        index: "name",
        value: "Jerry"
      ),
      if(
        fv!index = 3,
        a!update(
          data: index(local!data, 3, {}),
          index: "name",
          value: "Justine"
        ),
        fv!item
      )
    )
  )
)

csteward
cstewardAnswer
Brainy
Aug 13, 2021 9:04 PM UTC

As an alternative for prior versions of Appian, the Dictionary Manipulation plugin is very useful for these sorts of things:

a!localVariables(
  local!data: {
    { index: 1, name: "John", lastName: "Doe" },
    { index: 2, name: "Henry", lastName: "Doe" },
    { index: 3, name: "Jenny", lastName: "Doe" },
  },
  
  a!forEach(
    items: local!data,
    expression: if(
      fv!index=2,
      updatedictionary(
        fv!item,
        {
          name: "Jerry"
        }
      ),
      fv!item
    )
  )
)

Known Participant
Aug 14, 2021 1:36 AM UTC

Sorry for the late reply. I tried this 

if the index is 1 and 2 the value should be both updated. but its not working as expected on my end only one can be updated.