Skip to main content
daniels0002
Participating Frequently
September 26, 2024
Question

Trouble updating array with information from another array

"I'm encountering an issue with updating an array in costAdjustments. The current code is setting the LengthOfMonth field to 6 for all items in the array, but I only want to update specific entries."

a!save(
  local!newEstimates2,
  local!newEstimates2: a!forEach(
    items: local!newEstimates,
    expression: a!update(
      fv!item,
      "costAdjustments",
      a!forEach(
        items: fv!item.costAdjustments,
        expression: a!update(fv!item, "LengthOfMonth", 6)
      )
    )
  )
)

This isn't exactly what I need. What I want to do is loop through newEstimates and pull data from local!uniqueContractPeriods, which contains three rows of data with the fields: period, periodType, startDate, endDate, noOfMonths, and noOfDays. When the contractPeriodNum in costAdjustments within newEstimates matches the period in uniqueContractPeriods, I want to update the LengthOfMonth in newEstimates with the value of noOfMonths from uniqueContractPeriods. Below is the code I've written.

a!save(  
              local!newEstimates2,  
              local!newEstimates2: a!forEach(  
                items: local!newEstimates,  
                expression: a!update(  
                  fv!item,  
                  "costAdjustments",  
                  a!forEach(  
                    items: local!uniqueContractPeriods,  
                    expression: if(  
                      fv!item.period = fv!item.costAdjustments.contractPeriodNum,  
                      a!update(  
                        fv!item,  
                        "LengthOfMonth",  
                        index(  
                          local!uniqueContractPeriods.noOfMonths,  
                          where(  
                            tostring(fv!item.period) = tostring(fv!item.costAdjustments.contractPeriodNum)  
                          )  
                        )  
                      ),  
                      fv!item  
                    )  
                  )  
                )  
              )  
            )

I am sure it is a simple fix, but I have been working on it for a long time, any assistance would be great.  Thank you.

7 replies

kumara0180
Participating Frequently
September 26, 2024

without the actual values I am finding it hard to debug this code, But I would have tried using displayValue function. also your  

https://docs.appian.com/suite/help/24.3/fnc_conversion_displayvalue.html

daniels0002
Participating Frequently
September 26, 2024

Thank you, I will take a look at that.

kumara0180
Participating Frequently
September 27, 2024

Sure let me know if you able to achieve desired result. Also in your first approach, your 2 foreach items would be confusing each other. Use the local variables to refer the top level foreach 

stefanhelzle0001
Brainy
September 27, 2024

Your a!save() call is wrong. You cannot assign a value inside the value attribute of a!save, like you try in line 3. I suggest to check the correct syntax for a!save().

daniels0002
Participating Frequently
September 27, 2024

I got it to work but wonder if there is a better way to  write it.  I tried writing the updates all within curly braces, but that doesn't work.  It only updates one of them.  Can you take a look and suggest changes.

a!save(
              local!newEstimates2,
              a!forEach(
                items: local!newEstimates,
                expression: if(
                  a!isNullOrEmpty(
                    wherecontains(
                      fv!item.costAdjustments.contractPeriodNum,
                      tointeger(local!uniqueContractPeriods.period)
                    )
                  ),
                  fv!item,
                  a!update(
                    fv!item,
                    "costAdjustments",
                    a!update(
                      a!update(
                        a!update(
                          a!update(
                            fv!item.costAdjustments,
                            "lengthOfMonth",
                            cast(
                              1,
                              index(
                                local!uniqueContractPeriods.noOfMonths,
                                wherecontains(
                                  fv!item.costAdjustments.contractPeriodNum,
                                  tointeger(local!uniqueContractPeriods.period)
                                ),
                                null
                              )
                            )
                          ),
                          "periodStartDate",
                          index(
                            local!uniqueContractPeriods.startDate,
                            wherecontains(
                              fv!item.costAdjustments.contractPeriodNum,
                              tointeger(local!uniqueContractPeriods.period)
                            ),
                            null
                          )
                        ),
                        "periodEndDate",
                        index(
                          local!uniqueContractPeriods.endDate,
                          wherecontains(
                            fv!item.costAdjustments.contractPeriodNum,
                            tointeger(local!uniqueContractPeriods.period)
                          ),
                          null
                        )
                      ),
                      "LengthOfDay",
                      index(
                        local!uniqueContractPeriods.noOfDays,
                        wherecontains(
                          fv!item.costAdjustments.contractPeriodNum,
                          tointeger(local!uniqueContractPeriods.period)
                        ),
                        null
                      )
                    )
                  )
                )
              )
            )

stefanhelzle0001
Brainy
September 27, 2024

To resolve such issues, I try to simplify what I am doing. Create a separate expression with some local test data. Then try to understand how to use a!update. Then apply that new knowledge to your actual problem.

This is how I learned to code 40 years ago, and this is how I solve my problems in my last 15 years with Appian.