# Concat/collab dataset from multiple contracts and create another contract

**URL:** <https://forum.canton.network/t/concat-collab-dataset-from-multiple-contracts-and-create-another-contract/2748>\
**Category:** App Development\
**Tags:** daml\
**Created:** [June 23, 2021, 5:46pm UTC](https://forum.canton.network/t/concat-collab-dataset-from-multiple-contracts-and-create-another-contract/2748 "2021-06-23T17:46:14Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Gary\_Verhaegen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gary_verhaegen/32/81_2.png) [@Gary\_Verhaegen](https://forum.canton.network/u/Gary_Verhaegen)\
**Post date:** [June 24, 2021, 2:01pm UTC](https://forum.canton.network/t/concat-collab-dataset-from-multiple-contracts-and-create-another-contract/2748/9 "2021-06-24T14:01:56Z")

</div>

Ok, I can work with that. Here is some code that I believe does what you’re asking for here:

```haskell
module Main where

import Daml.Script

template ContractA
  with
    sig: Party
    id: Text
    argument: Text
    obs: Party
  where
    signatory sig
    observer obs
    key (sig, id) : (Party, Text)
    maintainer key._1

template ContractB
  with
    sig: Party
    collabData: [(Party, Text)]
  where
    signatory sig

setup : Script ()
setup = script do
  party1 <- allocatePartyWithHint "party1" (PartyIdHint "party2")
  party2 <- allocatePartyWithHint "party2" (PartyIdHint "party1")
  mainParty <- allocatePartyWithHint "mainParty" (PartyIdHint "mainParty")

  -- party1 creates ContractA (signatory party1, observer mainParty) with key (party1,keyData)
  submit party1 do
    createCmd ContractA with sig = party1, obs = mainParty, id = "keyData", argument = "some data"

  -- party2 creates ContractA (signatory party2, observer mainParty) with key (party2,keyData)
  submit party2 do
    createCmd ContractA with sig = party2, obs = mainParty, id = "keyData", argument = "some other data"

  -- mainParty
  submit mainParty do
    createAndExerciseCmd (DoCollection with sig = mainParty) CreateContractB with parties = [party1, party2], id = "keyData"
  return ()

template DoCollection
  with
    sig: Party
  where
    signatory sig
    preconsuming choice CreateContractB : ContractId ContractB
      with parties: [Party]
           id: Text
      controller sig
      do
        -- 1) iterate over given parties (here [party1,party2]), and
        -- 3)Then store it in variable results
        results <- forA parties (\party -> do
          -- 2) fetch the contract; note: there can only be (at most) one because
          -- keys enforce uniqueness
          (_, payload) <- fetchByKey @ContractA (party, id)
          -- If there is no contract with this key, or mainParty is not an observer,
          -- we crash here.

          -- 2) Once I get the respective contracts I will retrieve subset of data
          -- from fetched ContractA
          return (party, payload.argument))
        -- 4) Once all( in this case it’s just 2 ) the subset is stored in local
        -- variable call next contract (ContractB) with these dataset
        create ContractB with collabData = results, ..

```

---

_[View the full topic](https://forum.canton.network/t/concat-collab-dataset-from-multiple-contracts-and-create-another-contract/2748)._
