I encountered a problem when creating a formula where I wanted to use both optional parameters and vararg parameters. The use case is:
- Formula should add (create) an invoice in an external invoicing system.
- Some params should be required (e.g.
clientId
). - Some params can be optional and set to default values if omitted (e.g.
issueDate
which is set to today by default). - The invoice can include multiple items and each item has several subfields (e.g. name, quantity, price etc.). Those items should be also included as params in the formula.
The last requirement turned out to be the most problematic. The solutions I considered:
- Ideally, invoice items would be provided as objects but formulas don’t allow object params.
- So, I thought of having a set of array params, one for each invoice item field (e.g. list of names, list of quantities, list of prices etc.) but this doesn’t seem to be intuitive and good information architecture.
- Finally, I decided to use several vararg params, one for each invoice field item.
The problem is, Coda requires me to provide all optional params when I want to call the formula with invoice items (vararg params) and skip optional params. Otherwise, it assumes the vararg params represent optional params and shows “Incorrect named argument” error:
The formula code looks the following (I skipped some params to show the core concept):
pack.addFormula({
name: "AddInvoice",
description: "Add a new invoice.",
isAction: true,
resultType: coda.ValueType.Object,
schema: InvoiceSchema,
parameters: [
[...]
coda.makeParameter({
type: coda.ParameterType.Number,
name: "clientId",
description: "Client ID.",
}),
[...]
coda.makeParameter({
type: coda.ParameterType.String,
name: "issueDate",
description: "Invoice issue date. Default: today.",
optional: true,
}),
[...]
],
varargParameters: [
coda.makeParameter({
type: coda.ParameterType.Number,
name: "itemProductId",
description: "Product ID of the invoice item.",
}),
coda.makeParameter({
type: coda.ParameterType.Number,
name: "itemQuantity",
description: "Quantity of the invoice item.",
}),
coda.makeParameter({
type: coda.ParameterType.Number,
name: "itemTotalPriceGross",
description: "Total gross price of the invoice item.",
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "itemTax",
description: "Tax of the invoice item.",
}),
],
What solution would you recommend to make it possible to provide vararg params without the need to specify all the optional params too?