# Retrieving contract - issues

**URL:** <https://forum.canton.network/t/retrieving-contract-issues/5978>\
**Category:** App Development\
**Created:** [January 16, 2023, 12:24pm UTC](https://forum.canton.network/t/retrieving-contract-issues/5978 "2023-01-16T12:24:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![WallaceKelly](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/wallacekelly/32/5046_2.png) [@WallaceKelly](https://forum.canton.network/u/WallaceKelly)\
**Post date:** [January 17, 2023, 3:50pm UTC](https://forum.canton.network/t/retrieving-contract-issues/5978/2 "2023-01-17T15:50:44Z")

</div>

Hi, @claudia_giannoni! It appears you are making great progress!

Let’s address this first:

> I also do not understand from the documentation why I need and Helper template to call ‘fetchByKey’

I’m assuming you read [this thread](https://discuss.daml.com/t/in-daml-script-do-we-have-equivalent-command-for-lookupbykey-fetchbykey/919/2) in which cocreature explained that `fetchByKey` cannot be called from a Daml Script.

In summary, `fetchByKey` can only be used in a _choice_. It cannot be used in a _script_. Consequently, if you want to write a _script_ that retrieves a contract based on a key, you will need to first write a _choice_ that calls `fetchByKey`. That _choice_ needs to be on a template. You might as well name that template with “helper” in the name to clarify that its purpose is to help with some other template.

> I also do not understand from the documentation … how to use it.

The general idea of fetching a contract by key is that the key does not change… even as a contract is updated. When a contract is updated, the contract id changes. If you have defined a key on a contract that stays the same even as the contract is changed, then you can always retrieve the “current version” of the asset.

However, notice that in your code, the `UpdateAsset` choice allows the `idCode` to be changed. You probably don’t want to do that. The idea with keys is that they don’t change. You probably want to remove the `new_idCode` parameter from your `UpdateAsset` choice, so that the `idCode` can never be changed after the contract is initially created.

Additionally, I see in your code that you have a choice named `FetchAssetByKey` on the `Asset` template. That’s a good start, but does not actually make sense. The reason it does not make sense is that to exercise that `FetchAssetByKey` choice, you would need to already have the contract id of the asset. If you already have the contract id of the asset, you do not need to fetch it by key.

So imagine you don’t know the contract id of an asset (because the asset has been updated a couple times since it was created and gets a new contract id on every update). But, you do know the key (the `idCode` in your example).

That is where the concept of a “helper” comes into play. You need a whole new `AssetHelper` template, with a `FetchByKey` choice. Here is a sample implementation:

```haskell
template AssetHelper
  with
    caller : Party
  where
    signatory caller
    choice GetAssetByIdCode : (AssetId, Asset)
      with idCode : Text
      controller caller
      do
        fetchByKey @Asset (caller, idCode)

```

With the above defined, you can create new instances of the `AssetHelper` and exercise the `GetAssetByIdCode` choice whenever you want. Like this, for example:

```haskell
(currentAssetId, currentAsset) <- submit cms do
  createAndExerciseCmd (AssetHelper cms) GetAssetByIdCode with
    idCode = "XX2325XXX"

```

Notice the above expressions returns the current asset that has the key `XX2325XXX`. You can place the above `createAndExerciseCmd` in your script after the asset is initially created _and_ after the contract has been updated. In both cases, it will give you the “current” version of contract `XX2325XXX`.

Does that get you moving forward again?

---

_[View the full topic](https://forum.canton.network/t/retrieving-contract-issues/5978)._
