Problem with saving into array using a foreach

I have  an expression rule with the following code


ri!createdBy,
a!forEach(
items: ri!createdBy,
expression: rule!OBV_replaceNull(
fv!item,
loggedInUser()
)
)
),

I call it from an interface like this

rule!myER(createdby: cdtName.created_by), where cdtName is an array of cdt type

The problem is that every item in the resulting array contains all the values. So if it loops twice cdtname.created_by will look like this {username;username, username;username}. If I did just a save like this save(ri!createdBy, loggedInUser()) then it works fine. 

Running the for each alone seems to return the correct result within the expression rule. However, it is once I add the save into the rule input that causes problems. Has anyone run into this problem and found a solution?

  Discussion posts and replies are publicly visible

  • I think i didn't get the whole context but why don't you try to cast in the for each so every returning item would be of that CDT type.

    IE.

    a!forEach(
    items: local!yourVariable //whole cdt ?
    expression:
    'type!{urn:com:appian:types:TSS}TSS_Test'(

    id:fv!item.id,

    field2:fv!item.field2,

    username :rule!OBV_replaceNull(fv!item,loggedInUser()
    )

    )

  • The reason I can't do that it's because I'm not passing the whole CDT, I'm only passing one of the cdt fields (created_by) which is an array of text.

  • saveInto: {
    a!forEach(
    ri!cdtName,
    a!save(
    fv!item.createdBy,
    if(
    isnull(
    fv!item.createdBy
    ),
    loggedInUser(),
    fv!item.createdBy
    )
    )
    )
    }

    can you try like above 

  • 0
    Certified Lead Developer

    In the off chance that you're able to restructure things slightly (maybe, i'm a little fuzzy on what your current nested structure actually is), I was able to get the following example to work for essentially your use case.  Let me know if I got close at all ;-)

    (disclaimer: I acknowledge that this ended up being functionally identical to the example supplied above by Krishna, though I came up with this independently... and i guess the main benefit of mine is that it can be tested in a blank Interface editor without additional hassle).

    a!localVariables(
      local!testCdtArray: {
        {
          id: 1,
          user: "testUser1"
        },
        {
          id: 2,
          user: ""
        },
        {
          id: 3,
          user: "testUser2"
        }
      },
      
      a!sectionLayout(
        contents: {
          a!paragraphField(
            value: local!testCdtArray,
            readOnly: true()
          ),
          a!buttonArrayLayout(
            align: "START",
            buttons: a!buttonWidget(
              label: "test",
              saveInto: {
                a!forEach(
                  local!testCdtArray,
                  a!save(
                    fv!item.user,
                    if(isnull(fv!item.user), loggedInUser(), fv!item.user)
                  )
                )
              }
            )
          )
        }
      )
    )

  • That solution wouldn't work in my case. I was able to come up with a different solution that works for me. To add to your answer, I'm not trying to loop over the entire cdt, just one of the fields since that's all I'm passing into the expression rule. Using your code, something more like this. And somehow that returns duplicate values in each field of the cdt

    a!save(
    local!testCdtArray.user,
      a!forEach(
            local!testCdtArray.user,
            if(isnull(fv!item), loggedInUser(), fv!item)
        )
    )

  • 0
    Certified Lead Developer
    in reply to Jose H.

    Any chance you can provide more accurate sample code representing your nested rule structure?  I'd be willing to continue playing with it to see if I can help come up with something workable that still fulfills your use case, but as I said I'm a bit fuzzy on how your two current rules are actually interacting, and I don't think it would be very helpful for me to build something using completely blind guesswork.

    I was able to come up with a different solution that works for me.

    Oops, I missed this part.  Mind sharing what the change was, in case anyone runs into this issue in the future?