Error "Missing field: commands" when running trigger

I wrote a trigger with the following rule:

acceptRule : Party -> ACS -> Time -> Map CommandId [Command] -> s -> TriggerA ()
acceptRule party acs t pending s = do
  let proposals = getContracts @IssueProposal acs
  let pcids = map fst proposals
  let cmds = map (\cid -> exerciseCmd cid Accept) pcids
  let pendings = map (\c -> c.contractId) cmds
  emitCommands cmds pendings
  pure ()

When I try to run it I get the following message repeated indefinitely:

Command failed: Missing field: commands, code: 3

What am I doing wrong here?

I’m somewhat guessing here but I believe that cmds is probably empty. The ledger API does not accept empty list of commands. If that is the issue you can simply wrap it in when:

when (not (null cmds)) (emitCommands cmds pendings)

Might make sense to modify emitCommands to handle this automatically.

Thanks. When I try your suggestion I get a type mismatch:

Couldn't match type ‘CommandId’ with ‘()’
      Expected type: TriggerA ()
        Actual type: TriggerA CommandId

I changed it to:

  when (L.null cmds) do
    emitCommands cmds pendings
    pure ()

Is there a one-liner to do the same?

Ah good point, you can use the void function to turn this into a one-liner:

when (not $ null cmds) $ void $ emitCommands cmds pendings

Note the not. You seem to have lost that when you changed it.

Ah, thanks. Yep, missed the not but I’ve now ended up using unless anyway.

Good point, unless makes more sense here.