how can i set counter value after each iteration in forEach funtionin eg set counter = counter +1 or counter = 1 based on condition
Let just say
I have 2 CDT
itemA:{1,2,3}
itemB{c,d,e,f,g,h}
and i have to generate resultant CDT itemZ: [{ guid:c, ac:1} , { guid:d, ac:2} ,{ guid:e, ac:3} , { guid:f, ac:1} ,{ guid:g, ac:2} ,{ guid:h, ac:3} ]
In my expression rule I am iterating itemB using foreach function
local!counter:0,
local!ItemZ :foreach(
items:itemB
expression :a!localVariable(
local!:counter: if(length(ItemA)>= local!counter, local!:counter +1, 1),
if(length(ItemA)>= local!counter,
{
type!ItemZ
(
guid:fv!item,
ac:ItemA[counter]
)
},
/*else case */
{}
Issue is: every iteration counter is set to 0 instead of previous value of counter.
Discussion posts and replies are publicly visible
Hi sugasanr0001I think this code snippet will work fine for you as this generates the required Z CDT.
a!localVariables( local!a: {1,2,3}, local!b: {"c","d","e","f","g","h"}, local!sizeOfA: count(local!a), a!forEach( items: local!b, expression: { guide: fv!item, ac: local!a[ if( fv!index<=local!sizeOfA, fv!index, fv!index-((ceiling(fv!index/local!sizeOfA)-1)*local!sizeOfA) ) ] } ) )
Wow you are awesome. BIg thumbs up . I am still curious if i can reassign the local variable (defined outside foreach function) inside each iteration
No. You cannot do this. A local or any variable can only be updated inside a component's "SaveInto" ONLY. As we aren't using any interface component here, we won't be able to that.
OK thank you for quick response.