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.

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    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().

  • 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
                          )
                        )
                      )
                    )
                  )
                )

Reply
  • 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
                          )
                        )
                      )
                    )
                  )
                )

Children