# Optional params and vararg params in a single formula

**URL:** <https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207>\
**Category:** Making Packs\
**Created:** [November 6, 2023, 10:04pm UTC](https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207 "2023-11-06T22:04:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikolaj\_Pastuszko](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/mikolaj_pastuszko/32/14812_2.png) [@Mikolaj\_Pastuszko](https://connect.superhuman.com/u/Mikolaj_Pastuszko)\
**Post date:** [November 6, 2023, 10:04pm UTC](https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207/1 "2023-11-06T22:04:53Z")

</div>

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:

 ![CleanShot 2023-11-06 at 22.54.19@2x](https://global.discourse-cdn.com/coda1/original/3X/4/1/41544bb958f8e0224c25bc7aa5327c21ef0be057.png)

**The formula code** looks the following (I skipped some params to show the core concept):

```auto
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?

---

<div class="post-metadata">

**Author:** ![Eric\_Koleda](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/eric_koleda/32/50286_2.png) [@Eric\_Koleda](https://connect.superhuman.com/u/Eric_Koleda)\
**Post date:** [November 7, 2023, 12:22am UTC](https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207/2 "2023-11-07T00:22:37Z")

</div>

Hi @Mikolaj_Pastuszko - Welcome to the Coda community! Your assessment is correct, that once you add any vararg parameters you must specify all optional parameters. This is because vararg parameters can only be addressed positionally, not by name. There are probably ways to change the Coda Formula Language to address this in the long term, but at the moment this is an unavoidable consequence.

The approach I’ve recommended to other Pack makers is to move your vararg parameters to a separate helper formula, which bundles them up into JSON, and passes that JSON to a new optional parameter of your main formula. For example:

```auto
AddInvoice(My Account, "vat", 111, 111, 
  items: Items("abc", 10, 123.45, 20))

```

You could make the `Items()` formula use varargs, so you could specify multiple items at once:

```auto
AddInvoice(My Account, "vat", 111, 111, 
  items: Items("abc", 10, 123.45, 20, "def", 20, 45, 10))

```

But for this use it isn’t the easiest to read. It might make more sense to pass a `List()` of single `Item()` results.

```auto
AddInvoice(My Account, "vat", 111, 111, 
  items: List(Item("abc", 10, 123.45, 20), Item("def", 20, 45, 10)))

```

But if you expect these items to come from a table it probably makes more sense to make each parameter an `Array` or `SparseArray` type, so that the user could pass an entire column at once:

```auto
AddInvoice(My Account, "vat", 111, 111, 
  items: Items(ItemsTable.Name, ItemsTable.Quantity, ItemsTable.TotalPriceGross, ItemsTable.Tax))

```

@Leandro_Zubrezki’s [Quickbooks Pack](https://coda.io/packs/quickbooks-10759) ran into this same issue, and he went with something sort of like the 2nd option I showed.

---

<div class="post-metadata">

**Author:** ![Mikolaj\_Pastuszko](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/mikolaj_pastuszko/32/14812_2.png) [@Mikolaj\_Pastuszko](https://connect.superhuman.com/u/Mikolaj_Pastuszko)\
**Post date:** [November 7, 2023, 11:13pm UTC](https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207/3 "2023-11-07T23:13:33Z")

</div>

@Eric_Koleda Thanks a lot, this looks like a great solution! 🙏 Very clear and easy to read!

---

<div class="post-metadata">

**Author:** ![Dan\_Guberman](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/dan_guberman/32/29360_2.png) [@Dan\_Guberman](https://connect.superhuman.com/u/Dan_Guberman)\
**Post date:** [January 12, 2024, 4:43pm UTC](https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207/4 "2024-01-12T16:43:39Z")

</div>

@Eric_Koleda, could you elaborate on how to define a formula parameter for your third case:

```auto
AddInvoice(My Account, "vat", 111, 111, 
  items: List(Item("abc", 10, 123.45, 20), Item("def", 20, 45, 10)))

```

I was trying to figure out how to pass variable number of parameters at run-time (i.e. not statically) to a formula, but thought that Coda didn’t have arrays of objects as parameters. Maybe I misunderstood your approach?

---

<div class="post-metadata">

**Author:** ![Eric\_Koleda](https://yyz1.discourse-cdn.com/coda1/user_avatar/connect.superhuman.com/eric_koleda/32/50286_2.png) [@Eric\_Koleda](https://connect.superhuman.com/u/Eric_Koleda)\
**Post date:** [January 12, 2024, 7:33pm UTC](https://connect.superhuman.com/t/optional-params-and-vararg-params-in-a-single-formula/44207/5 "2024-01-12T19:33:57Z")

</div>

@Dan_Guberman - That’s correct, Packs can’t accept objects as parameters. In that example the `Item()` formula would output a string of JSON, and the `items` parameter would be of type `StringArray` so it could accept multiple JSON strings.

You can see this pattern used in my [Mockaroo Pack](https://github.com/erickoledadevrel/packs/blob/main/mockaroo/pack.ts). Take a look at the `MockColumn` formula and the `columns` parameter of the `MockData` sync table.
