# Exercise choice based on template instance

**URL:** <https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886>\
**Category:** App Development\
**Tags:** daml\
**Created:** [January 5, 2021, 9:27am UTC](https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886 "2021-01-05T09:27:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arne\_Gebert](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/arne_gebert/32/1213_2.png) [@Arne\_Gebert](https://forum.canton.network/u/Arne_Gebert)\
**Post date:** [January 5, 2021, 9:27am UTC](https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886/1 "2021-01-05T09:27:45Z")

</div>

I have an instance t of a template T and want to exercise a choice on it.  
However, the [exercise function](https://docs.daml.com/daml/stdlib/Prelude.html#function-da-internal-template-functions-exercise-24549) requires a ContractId T argument.

Is there a way I can exercise a choice based on just the template instance t?

---

<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:** [January 5, 2021, 9:53am UTC](https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886/2 "2021-01-05T09:53:32Z")

</div>

Short answer is “no”. Here is a slightly longer explanation.

A DAML program is a piece of code that interacts with a remote ledger. Contracts are things that exist on the ledger, and, crucially, only on the ledger; you never have a contract in your (running) DAML program. What you can have in your DAML program is one of two things:

- The _payload_ of a contract, i.e. a piece of data that holds the same fields as the contract does. The shape of that data structure is defined implicitly by a template declaration, but as far as your DAML program is concerned, it really is the same as if you had defined the same fields with a `data` declaration. Except for the fact that you typically create such a piece of data based on what you recently read from the ledger, there is actually no relationship between that piece of data and any contract on the (remote, off-program) ledger.
- The _contract ID_ of a contract, which has no meaning by itself and is just an opaque token given to you by the ledger, which you can give back to the ledger at some future point as part of a command that should use said contract.

If you do not have the contract ID, but just a random data structure with the same fields as the contract has on the ledger, in the general case you cannot interact with the "equivalent"contract on the ledger, even should one actually exist. (Though how do you define that? There could be multiple ones with all the same values.).

In the special case where your template defined a key, you can of course reconstruct said key from your payload, but the contract the key points to may not be the same one you got the payload from, as contract keys are essentially mutable references.

Hope that helped.

---

<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:** [January 5, 2021, 9:58am UTC](https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886/3 "2021-01-05T09:58:42Z")

</div>

If you have sufficient authority, you can use [`createAndExercise`](https://docs.daml.com/daml/stdlib/Prelude.html#function-da-internal-template-functions-createandexercise-50131). It’s really just shorthand for

```haskell
do
  cid <- create t
  exercise cid c

```

---

<div class="post-metadata">

**Author:** ![Arne\_Gebert](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/arne_gebert/32/1213_2.png) [@Arne\_Gebert](https://forum.canton.network/u/Arne_Gebert)\
**Post date:** [January 5, 2021, 10:04am UTC](https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886/4 "2021-01-05T10:04:37Z")

</div>

Thanks both! The solution I ended up going for was adding a key to my contract template and fetching the ContractId based on the key.

---

<div class="post-metadata">

**Author:** ![Stephen](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/stephen/32/102_2.png) [@Stephen](https://forum.canton.network/u/Stephen)\
**Post date:** [January 7, 2021, 2:36am UTC](https://forum.canton.network/t/exercise-choice-based-on-template-instance/1886/5 "2021-01-07T02:36:32Z")

</div>

In that case, you may enjoy [`exerciseByKey`](https://docs.daml.com/1.8.0/daml/stdlib/Prelude.html#function-da-internal-template-functions-exercisebykey-2276).
