# Testing fetchByKey

**URL:** <https://forum.canton.network/t/testing-fetchbykey/5947>\
**Category:** App Development\
**Created:** [January 12, 2023, 2:10pm UTC](https://forum.canton.network/t/testing-fetchbykey/5947 "2023-01-12T14:10:26Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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 12, 2023, 4:54pm UTC](https://forum.canton.network/t/testing-fetchbykey/5947/4 "2023-01-12T16:54:57Z")

</div>

First, let’s address what it would take to get your code to compile. Secondly, we can address the overall design.

* * *

Your code does not compile as-is. The problem is in the test. Specifically the following does not work:

```haskell
    submit cms do 
        createCmd SellAsset with
            
            asset = asset.Asset
            newOwnerSolicitor = mishconDeReya
            newOwner = silvia
            notes = "Questa villa e' stata venduta"

```

The above does not work because `asset` is not defined in this script and so `asset.Asset` fails. To get the test to work with the current template design, you could create an Asset contract first before trying to create the `SellAsset`.

> **Example implementation**
>
> ```haskell
> module Main_I where
> import Asset 
> import SellAsset
> 
> import Daml.Script
> 
> sell_asset_test : Script SellAssetId
> sell_asset_test = script do
> 
> cms <- allocatePartyWithHint "CMS" (PartyIdHint "CMS")   
> claudia <- allocatePartyWithHint "Claudia" (PartyIdHint "Claudia")  
> christie<- allocatePartyWithHint "Christie" (PartyIdHint "Christie") 
> claudia_Budha <- allocatePartyWithHint "Claudia_Budha" (PartyIdHint "Claudia_Budha") 
> mishconDeReya <- allocatePartyWithHint "MishconDeReya" (PartyIdHint "MishconDeReya")   
> silvia <- allocatePartyWithHint "Silvia" (PartyIdHint "Silvia")  
> time <- getTime
> 
> assetId <- submit claudia do
> createCmd Asset with
> ownerSolicitor = claudia
> owner = claudia
> auctionHouse = christie
> address = "1234 Address"
> idCode = "Asset 2345" 
> a_description = "Asset 2345 description"
> a_documents = []
> a_docLink = ""
> v_description = ""
> v_documents = []
> v_docLink = ""
> date = time
>     
> Some asset <- queryContractId claudia assetId
> 
> sellAsset <- submit claudia do 
> createCmd SellAsset with
> asset = asset
> newOwnerSolicitor = mishconDeReya
> newOwner = silvia
> notes = "Questa villa e' stata venduta"
>     
> submit claudia do
> exerciseCmd sellAsset AssetDocOffer_GetAsset
>     
> return sellAsset
> 
> ```

* * *

However, you might want to take another look at the overall design. Your `SellAsset` template includes a member of type `Asset`. That means, the data for the `Asset` is stored directly into your `SellAsset` template. Typically, you would have an `Asset` contract on the ledger. And then when you create the `SellAsset`, you would not include the entire `Asset` instance within the `SellAsset`. Instead you would reference the asset with either:

- a `ContractId Asset`, in which case you could retrieve the Asset with `fetch`.
- the values used by the `Asset`’s key (for example, `idCode`), in which case you could retrieve the Asset with `fetchByKey`.

Here is a start at what `SellAsset` would look like, referencing the `Asset` by contract id:

```haskell
template SellAsset
    with
        -- asset : Asset
        assetId : ContractId Asset
        ownerSolicitor : Party
        owner : Party
        auctionHouse : Party

        newOwnerSolicitor:Party
        newOwner:Party
        notes: Text

```

With the above, you would get the `Asset` using `fetch`.

Here is a start at what `SellAsset` would look like, referencing the `Asset` by key:

```haskell
template SellAsset
    with
        -- asset : Asset
        idCode : Text
        ownerSolicitor : Party
        owner : Party
        auctionHouse : Party

        newOwnerSolicitor:Party
        newOwner:Party
        notes: Text

```

With the above, you would get the `Asset` using `fetchByKey`.

I hope that is helpful.

---

_[View the full topic](https://forum.canton.network/t/testing-fetchbykey/5947)._
