"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."
costAdjustments
LengthOfMonth
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.
newEstimates
local!uniqueContractPeriods
contractPeriodNum
period
uniqueContractPeriods
noOfMonths
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
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 ) ) ) ) ) )
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.
Stefan, Thanks you so much. I still feel new at Appian development. I wish I had more development time. I am the only developer and sometimes I have questions. But I really appreciate you taking time to help.