# Nested MapA for Triggers to Exercise a choice

**URL:** <https://forum.canton.network/t/nested-mapa-for-triggers-to-exercise-a-choice/4260>\
**Category:** App Development\
**Tags:** trigger, damlhub\
**Created:** [April 6, 2022, 7:25am UTC](https://forum.canton.network/t/nested-mapa-for-triggers-to-exercise-a-choice/4260 "2022-04-06T07:25:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Max](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/max/32/2128_2.png) [@Max](https://forum.canton.network/u/Max)\
**Post date:** [April 6, 2022, 7:25am UTC](https://forum.canton.network/t/nested-mapa-for-triggers-to-exercise-a-choice/4260/1 "2022-04-06T07:25:46Z")

</div>

I’m trying to exercise the choice “Invite\_New\_Asset\_Holder” choice on a specific `AssetHoldingAccount` contract, where the symbol is “ET”

I can get a list of parties, but I’m not sure how to map through that list of parties, and exercise the “Invite\_New\_Asset\_Holder” with the parties as the argument of that choice.

```haskell
module Trigger where

import Account
import qualified Daml.Trigger as T
import DA.Foldable
import DA.Next.Map (Map)
import DA.Optional (whenSome)
import DA.Action
import qualified DA.List.Total as List

autoSendExampleAssetAccountProposal: T.Trigger ()
autoSendExampleAssetAccountProposal = T.Trigger 
 { initialize = pure (),
  updateState = \_ -> pure (),
  registeredTemplates = T.AllInDar,
  rule = \p -> do
    asset_holding_account_requests <- T.query @Account.AssetHoldingAccountRequest
    let isNotMe = (\requests -> requests.recipient /= p)
    let notMeList = filter (\(_, contract) -> isNotMe contract) asset_holding_account_requests
    let requests = map snd notMeList
    let recipients = map (\r -> r.recipient) requests
    debug recipients
    -- a party can have multiple AssetHoldingAccounts, however I only want to exercise the contract where - 
     --account.assetType.symbol == "ET
    assetAccounts <- T.query @Account.AssetHoldingAccount
    let isET = (\account -> account.assetType.symbol == "ET")
    let etAccounts = filter (\(_, contract) -> isET contract) assetAccounts
    -- the length of this should be one, always
    let cids = map fst etAccounts

     -- Somewhere here, I need to emitCommands I believe, because right now, I can log everything I need to in debug, but the ledger doesn't update with the result from exercising the choice. 

    let doSomething = map(\c -> map(\r -> T.exerciseCmd c Invite_New_Asset_Holder with recipient = r) recipients ) cids
    

    debug $ "TRIGGERED",
  heartbeat = None

}

```

Any pointers on what I should be doing?

---

<div class="post-metadata">

**Author:** ![bernhard](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/bernhard/32/356_2.png) [@bernhard](https://forum.canton.network/u/bernhard)\
**Post date:** [April 6, 2022, 8:15am UTC](https://forum.canton.network/t/nested-mapa-for-triggers-to-exercise-a-choice/4260/2 "2022-04-06T08:15:27Z")

</div>

Your `doSomething` looks like it has type `[[Command]]`, which is a good place to be. You now need to decide how you want to execute those commands. Each `emitCommands` results in one ledger transaction.

Do you want them all to be executed in one big transaction?

```haskell
emitCommands (concat doSomething) []

```

Do you want them to be batched per account?

```haskell
mapA (\cs -> emitCommands cs []) doSomething

```

Or do you want to run each command in its own transaction?

```haskell
mapA (\c -> emitCommands [c] []) (concat doSomething)

```

---

<div class="post-metadata">

**Author:** ![Max](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/max/32/2128_2.png) [@Max](https://forum.canton.network/u/Max)\
**Post date:** [April 7, 2022, 7:12am UTC](https://forum.canton.network/t/nested-mapa-for-triggers-to-exercise-a-choice/4260/3 "2022-04-07T07:12:33Z")

</div>

Fixed the issue with the looping of the trigger (or perhaps duplicated commands in this case, since it was resolved with some `unless` and `T.dedupExercise`

```haskell
autoSendExampleAssetAccountProposal: T.Trigger ()
autoSendExampleAssetAccountProposal = T.Trigger 
 { initialize = pure (),
  updateState = \_ -> pure (),
  registeredTemplates = T.RegisteredTemplates [T.registeredTemplate @Account.AssetHoldingAccountRequest, T.registeredTemplate @Account.AssetHoldingAccount],
  rule = \p -> do
    asset_holding_account_requests <- T.query @Account.AssetHoldingAccountRequest
    let isNotMe = (\requests -> requests.recipient /= p)
    let notMeList = filter (\(_, contract) -> isNotMe contract) asset_holding_account_requests
    let requests = map fst notMeList

    debug ("requests",requests)
    assetAccounts <- T.query @Account.AssetHoldingAccount
    debug assetAccounts
    let isET = (\account -> account.assetType.symbol == "ET" && account.assetType.issuer == p)
    let etAccounts = filter (\(_, contract) -> isET contract) assetAccounts
    let cids = map fst etAccounts
    
    
    unless ( DA.Foldable.null requests && DA.Foldable.null cids ) do
      let (cid, c) = head etAccounts

      mapA_(\request -> T.dedupExercise request Accept with assetHoldingAccount = cid) requests
      pure()
    debug $ "TRIGGERED",
  heartbeat = None
}

```
