Can I use where clause with the choice

Hello Team,
Can I use the where clause with Choices?
Let’s say I want to execute one choice only when the status is approved.

No, there is no syntax for “this choice is only valid under these conditions”.

You have two options here. The first, in my opinion better, one is to make multiple templates. If, based on the “state” of a contract it can have different sets of available choices, I’d make a separate template for each “state / set of choices”. That will make things a lot clearer for all the other code that interacts with that contract.

(Note that I’m using “contract” a bit loosely here, to designate a conceptual entity that, over time, may take different values for its attributes, and therefore will be represented, at the Daml level, by a succession of “contracts” in the “template instance” technical sense.)

If that is not an option for whatever reason, you can fail early in a choice if some conditions are not meant by using the assert function.

1 Like

First of all, this style of thinking, of choices being an implicative antecedent -> Update consequent is a good one to have. I do have a few comments though:

Although as Gary says, if you end up with your choices all being of the form test-flag -> do thing then what you really have is two or more templates that have been shoehorned into one using a boolean flags, and you would be better off keeping them separate.

Also, “stringly-typed” flags, such as you are using, are a code smell. They take up a lot of space in the contract, and there is no help from the compiler to ensure you don’t make a mistake with them. Datatypes are free, and it is trivial to convert string flags such as these into a simple datatype that is then correct by construction.

So assuming the bulk of the choices on this template are not predicated on these flags, I would suggest something along the lines of:

data LoanRequestStatus
  = NotApproved
  | Approved

data DueDiligenceStatus
  = Pending
  | InProgress
  | Completed

choice ProcessDueDiligence : ContractId LoanRequest
  controller recipient
  do
    assertMsg "Can only complete due diligence on approved loan requests" $ status == Approved
    create this with dueDiligence = Completed