# Formulae via API?

**URL:** <https://community.fibery.io/t/formulae-via-api/3617>\
**Category:** API & Programming\
**Created:** [November 29, 2022, 4:02pm UTC](https://community.fibery.io/t/formulae-via-api/3617 "2022-11-29T16:02:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 29, 2022, 4:02pm UTC](https://community.fibery.io/t/formulae-via-api/3617/1 "2022-11-29T16:02:33Z")

</div>

Hi, is it possible to:

- Update a text field so as to change it to a formula field?
  - (Or does one need to create a field as a formula field from its inception?)

- Define the formula for a field using textual notation such as `Entity.Field` rather than e.g. `[field_id field_id]`

…via the API?

Thanks!

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 29, 2022, 4:35pm UTC](https://community.fibery.io/t/formulae-via-api/3617/2 "2022-11-29T16:35:03Z")

</div>

> [@avi](#):
>
> is it possible to:
> 
> - Update a text field so as to change it to a formula field?

In principle, yes, it is possible (although it is not an approved method, so could break/be deprecated without warning!)

> [@avi](#):
>
> is it possible to:
> 
> - Define the formula for a field using textual notation such as `Entity.Field` rather than e.g. `[field_id field_id]`

Not via the API. The formula is defined using a query language that utilises field IDs, parameters, operators, etc… There’s no user-friendly way of doing it ☹

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 29, 2022, 5:14pm UTC](https://community.fibery.io/t/formulae-via-api/3617/3 "2022-11-29T17:14:41Z")

</div>

> [@Chr1sG](#):
>
> In principle, yes, it is possible (although it is not an approved method, so could break/be deprecated without warning!)

Thanks! Any chance you could share an example? This isn’t in the docs. I understand it would be an unsupported method/command/approach that might break; that’s fine with me.

> [@Chr1sG](#):
>
> Not via the API. The formula is defined using a query language that utilises field IDs, parameters, operators, etc… There’s no user-friendly way of doing it ☹

I figured that was the case, but I thought it couldn’t hurt to ask. Thanks!

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 29, 2022, 5:39pm UTC](https://community.fibery.io/t/formulae-via-api/3617/4 "2022-11-29T17:39:16Z")

</div>

> [@avi](#):
>
> Any chance you could share an example?

Something like the following code will convert a text field to a formula field (that has text as the return type)

```auto
const fibery = context.getService('fibery');

await fibery.executeSingleCommand({
    "command": "fibery.schema/batch", "args": {
        "commands": [{
            "command": "schema.field/set-meta",
            "args": {
                "holder-type": "SPACENAME/DBNAME",
                "name": "SPACENAME/TextField",
                "key": "formula/formula?",
                "value": "true",
            }
        },
        {
            "command": "schema.field/set-meta",
            "args": {
                "holder-type": "SPACENAME/DBNAME",
                "name": "SPACENAME/TextField",
                "key": "fibery/readonly?",
                "value": "true"
            }
        },
        {
            "command": "schema.field/set-meta",
            "args": {
                "holder-type": "SPACENAME/DBNAME",
                "name": "SPACENAME/TextField",
                "key": "formula/formula",
                "value": {
                    expression: [
                        'q/if',
                        [
                            '=',
                            ['370a66a8-686b-4955-8527-31b5f9e779c4'],
                            '$formulaParam1'
                        ],
                        '$formulaParam2',
                        '$formulaParam3'
                    ],
                    params: {
                        '$formulaParam1': 'hello',
                        '$formulaParam2': 'open',
                        '$formulaParam3': 'closed'
                    }
                }
            }
        }]
    }
})

```

In this example, the formula expression results in the following:  
`If(Name = "hello","open","closed")`

---

<div class="post-metadata">

**Author:** ![avi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/avi/32/5680_2.png) [@avi](https://community.fibery.io/u/avi)\
**Post date:** [November 29, 2022, 5:58pm UTC](https://community.fibery.io/t/formulae-via-api/3617/5 "2022-11-29T17:58:55Z")

</div>

That’s perfect. Thanks so much!

---

<div class="post-metadata">

**Author:** ![derbenoo](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/derbenoo/32/11540_2.png) [@derbenoo](https://community.fibery.io/u/derbenoo)\
**Post date:** [July 7, 2024, 8:25am UTC](https://community.fibery.io/t/formulae-via-api/3617/6 "2024-07-07T08:25:32Z")

</div>

@ChrisG thanks for providing this example API request! 🙏 However, I am getting the following error from the Fibery API:

> Modification of “formula/formula” field meta for “SPACENAME/Name” field of “SPACENAME/DBNAME” database demands ‘ **setup-formula** ’ claim.

I was not able to find any documentation regarding the “setup-formula” claim, has anybody an idea what this is about? 🙃

Thanks,  
Ben

* * *

Here is the full API response:

```auto
{
  "success": false,
  "result": {
    "name": "commands.error/schema-field-set-formula-meta-failed",
    "message": "Modification of \"formula/formula\" field meta for \"SPACENAME/Name\" field of \"SPACENAME/DBNAME\" database demands 'setup-formula' claim.",
    "data": {
      "top": {
        "error/name": "commands.error/schema-field-set-formula-meta-failed",
        "holder-type": "SPACENAME/DBNAME",
        "field": "SPACENAME/Name",
        "key": "formula/formula"
      },
      "cause": {
        "error/name": "commands.error/schema-field-set-formula-meta-failed",
        "holder-type": "SPACENAME/DBNAME",
        "field": "SPACENAME/Name",
        "key": "formula/formula"
      },
      "holder-type": "SPACENAME/DBNAME",
      "field": "SPACENAME/Name",
      "key": "formula/formula"
    }
  }
}

```
