How can I use a specific contract Id which exists on the ledger (within a script)?

G-d willing

Hello,
I have a DAML project that is installed on Daml Hub. This project includes a trigger as well.
The purpose of the trigger is to execute a function whenever a specific contract is created.
Currently, there is a problem within the function the trigger is calling. So, in order to find the problem, I would like to debug it. But I don’t want to do it by creating the contract - that will force the trigger to work.

I would like to write a script that runs this function. But I don’t know how can I provide it to the script as an
argument.
Can you please advise me on how to do it?

Hi @cohen.avraham,

Currently there is no way to build a ContractId a value with an arbitrary value, but what you can do is to utilize the --input-file argument of daml script. If you really only need the contract ID your json file should contain the contract ID you want to utilize in quotes (a simple JSON string).
Example:

"003c54de4a5e32f59997a4ca05c5ec3f6dbcab9d679937e3adedb7b69163fc5364ca001220250d1801527d7b961775ce9d3a3be00c597afd69c25f0419776298e979ea8ea6"

Such an input file can be utilized with a script that has the type ContractId a -> Script b. For example:

printIouCID : ContractId Iou -> Script()
printIouCID cid = do
  debug cid

You can also generate such files with the --output-file option. There is a simple party allocation example using these options in the Daml Script page of the docs.

Kind Regards,
Mate

1 Like

G-d willing

Thanks @Mate_Varga for your answer.
Let’s say that the contract id of the template is as you wrote:

"003c54de4a5e32f59997a4ca05c5ec3f6dbcab9d679937e3adedb7b69163fc5364ca001220250d1801527d7b961775ce9d3a3be00c597afd69c25f0419776298e979ea8ea6"

I need to fetch the contract’s data according to its key. Fetch can be called from within a choice, so let’s say I will add it. But still, how can I convert this Text value to be a real ContractId of a specific type, so once I will convert it, I will be able to call the fetch command?

You cannot convert form text to ContractId in Daml code. The conversion happens when daml script reads the input file, the type definition of the script you invoke will determine that what is expected to be converted from the data defined in the JSON file. So in the example I gave you, the string gets converted to a ContractID Iou because the script that is invoked with --input-file has type ContractId Iou -> Script().

You do not need to add fetch to a choice, you can get the contract’s data in the script either by using the ID or the key, calling the queryContractId or queryContractKey functions accordingly.

1 Like

G-d willing

Now I got you!. Okay. thank you very much, I will try that out and will let you know if I had some issues with it.