Combine 2 list into 1 with all the attributes

Certified Associate Developer

Hello guys,

i am trying to combine my list of contract numbers with contract line attributes, where a contract line can have multiple lines.

So for example my list of contracts (local!contracts) contains attribute like name, id, etc...

The contract line items (local!contractlines) contains different attributes of a contract like chargetype,currency,lineId, amount, etc... If there will be only 1 line of contract i can easily achieve the required output using a!forEach like this:

local!finaloutput: a!foreach(

                           items: local!contracts,

                           expression: {

                          {

                           contractName: fv!item.contractName,,

                           lineId: local!contractlines.lineId,

                           chargeType: local!contractlines.chargeType,

                           currency: local!contractlines.currency,

                           amount: local!contractlines.amount

                           }

                           }

Its providing the expected output only in case local!contractlines has just 1 line (1row). In case there are more rows the 2nd.3rd,etc.... are not taken under consideration. So if there are 2 contracts and 2 rows of line items the expected output will contain 4 items (4 rows) where the contrac number1 will have all the attributes of row1 and then row2 and then the same applies for contract number 2.

Thanks for any suggestions

                

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

    Please post a reusable example of what you're trying to do (set up some arrays in local variables to simulate your use case, such that anyone can copy-paste your code into a blank Expression Editor window and get what you're seeing).  Make sure to use the Code Box functionality.

  • 0
    Certified Lead Developer

    I think we might be getting into the wonderful world of nesting forEach loops.

    Let's just try to say what you want to achieve using the words "for each".

    For each contract, you want to apply the contract data for each of the contract lines?  I assume you want the cartesian product to come out, based on what you said (i.e. 10 contracts and 20 contract lines would spit out 200 combinations, all 20 lines for each of the 10 contracts).

    a!forEach(
        items: local!contracts,
        expression: a!localVariables(
            local!thisContract = fv!item, /*monster confusion when you try fv!item without this*/
            a!forEach( /*again*/
                items: local!contractLines,
                expression: a!update( 
                    data: fv!item,
                    index: {"field1", "field2", ...},
                    value: {local!thisContract.field1, local!thisContract.field2, ...}
                )
            )
        )
    )

  • 0
    Certified Associate Developer
    in reply to Dave Lewis

    working perfect, thank you