I am looping over a list and want to create a struct of struct if a struct key doesn't exist yet, but if it does exist, I want to only update one of the keys with a new value by adding something to the existing value of that key. It will not do that and I don't know why.
Here's the code:
<cfloop list="#recipeItemList#" index="ril">
<cfset thisrecipe = entityLoadByPK("recipes", #ril#)>
<cfloop array="#thisrecipe.getrecipeitem()#" index="recipeItems">
<cfif recipeItems.hasrawMaterial_id()>
<cfset theitem = entityLoadByPK("rawMaterials", recipeItems.getrawMaterial_id().getid())>
<cfinvoke method="getPrepByDate" component="/FEcomponents.production" returnvariable="makeAmount">
<cfinvokeargument name="recipe_id" value="#ril#">
<cfinvokeargument name="delivery_date" value="#delivery_date#">
</cfinvoke>
<cfset thisRecipeYield = #thisRecipe.getYield()#>
<cfif thisRecipeYield neq 0>
<cfset recipeRatio = makeAmount / thisRecipeYield>
</cfif>
<cfset thisQuantity = recipeRatio * #recipeItems.getitemQuantity()#>
<cfif structKeyExists(outerstruct, '#theitem.getid()#')>
<!--- we must add this quantity to the existing key, not create a new one --->
<cfset addthis = #outerstruct[theitem.getid()].totalToGet# + #thisQuantity#>
<cfset structInsert(outerstruct[theitem.getid()],'totalToGet', #addthis#,true)>
<!--- this is just to for testing to see if the value has changed, and it does not the 'addthis' value is always the latest value in the loop--->
#outerstruct[theitem.getid()].totalToGet# + #thisquantity# = #addthis# <BR>
<cfelse>
<!--- create a new struct --->
<cfset outerstruct[theitem.getid()] = structnew()>
<cfset outerstruct[theitem.getid()].rawMaterialid = #theitem.getid()#>
<cfset outerstruct[theitem.getid()].rawMaterialName = #theItem.getName()#>
<cfset outerstruct[theitem.getid()].totalToGet = #thisQuantity#>
<cfset outerstruct[theitem.getid()].uom = #theItem.getUOM().getname()#>
</cfif>
</cfif>
</cfloop>
</cfloop>
What am I doing wrong.
Thanks for your help
I think this will do what you want, I removed a few of the extra pound signs and changed the way you are updating the struct key. I'm sorry I am unable to run the code but I believe this should work.
<cfloop list="#recipeItemList#" index="ril">
<cfset thisrecipe = entityLoadByPK("recipes", #ril#)>
<cfloop array="#thisrecipe.getrecipeitem()#" index="recipeItems">
<cfif recipeItems.hasrawMaterial_id()>
<cfset theitem = entityLoadByPK("rawMaterials", recipeItems.getrawMaterial_id().getid())>
<cfinvoke method="getPrepByDate" component="/FEcomponents.production" returnvariable="makeAmount">
<cfinvokeargument name="recipe_id" value="#ril#">
<cfinvokeargument name="delivery_date" value="#delivery_date#">
</cfinvoke>
<cfset thisRecipeYield = thisRecipe.getYield()>
<cfset thisQuantity = recipeItems.getitemQuantity()>
<cfif thisRecipeYield neq 0>
<cfset recipeRatio = makeAmount / thisRecipeYield>
<cfset thisQuantity *= recipeRatio>
</cfif>
<cfif structKeyExists(outerstruct, theitem.getid())>
<!--- we must add this quantity to the existing key, not create a new one --->
<cfset outerstruct[theitem.getid()].totalToGet += thisQuantity>
<cfelse>
<!--- create a new struct --->
<cfset outerstruct[theitem.getid()] = structnew()>
<cfset outerstruct[theitem.getid()].rawMaterialid = theitem.getid()>
<cfset outerstruct[theitem.getid()].rawMaterialName = theItem.getName()>
<cfset outerstruct[theitem.getid()].totalToGet = thisQuantity>
<cfset outerstruct[theitem.getid()].uom = theItem.getUOM().getname()>
</cfif>
</cfif>
</cfloop>