{"id":31202052,"url":"https://github.com/programmfabrik/fylr-plugin-formula-columns","last_synced_at":"2025-09-20T13:05:11.528Z","repository":{"id":65416744,"uuid":"545993932","full_name":"programmfabrik/fylr-plugin-formula-columns","owner":"programmfabrik","description":null,"archived":false,"fork":false,"pushed_at":"2025-07-31T09:23:23.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-31T13:00:15.702Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/programmfabrik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-10-05T10:37:50.000Z","updated_at":"2025-07-31T09:22:07.000Z","dependencies_parsed_at":"2025-07-22T13:09:08.517Z","dependency_job_id":"290e38e7-c1bf-4982-9e25-218d0db321e4","html_url":"https://github.com/programmfabrik/fylr-plugin-formula-columns","commit_stats":null,"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"purl":"pkg:github/programmfabrik/fylr-plugin-formula-columns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmfabrik%2Ffylr-plugin-formula-columns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmfabrik%2Ffylr-plugin-formula-columns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmfabrik%2Ffylr-plugin-formula-columns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmfabrik%2Ffylr-plugin-formula-columns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/programmfabrik","download_url":"https://codeload.github.com/programmfabrik/fylr-plugin-formula-columns/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/programmfabrik%2Ffylr-plugin-formula-columns/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":276099416,"owners_count":25584992,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-20T02:00:10.207Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-09-20T13:03:31.597Z","updated_at":"2025-09-20T13:05:11.522Z","avatar_url":"https://github.com/programmfabrik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fylr-formula-columns-plugin\n\nPlugin for FYLR to store computed values in most column types using a small Javascript snippet.\n\n## Installation\n\nThe latest plugin can be download from [Github](https://github.com/programmfabrik/fylr-plugin-formula-columns/releases/latest/download/fylr-plugin-formula-columns.zip). Latest release notes can be found [here](https://github.com/programmfabrik/fylr-plugin-formula-columns/releases).\n\n## Web frontend\n\nAfter activation of the plugin, the web frontend shows custom settings for \"formula columns\" in \"Data model \u003e Object type \u003e Columns \u003e Option\".\n\nThere a Javascript can be defined which calculates the value of the column during the \"db_pre_save\" callback phase which runs on /api/db before objects are inserted into the database. A \"debug\" option exists to store an event of type \"FORMULA_COLUMNS_DEBUG\" which helps debugging.\n\nIf the formula throws an Exception, an event of type \"FORMULA_COLUMNS_ERROR\" is stored.\n\n## /api/schema\n\nIn /api/schema the custom setting looks like this:\n\n```json\n{\n    \"custom_setting\": {\n        \"formula-columns\": {\n            \"debug\": false,\n            \"script\": \"... snippet ...\"\n        }\n    }\n}\n```\n\n\n## /api/db\n\nThe plugin works as `db_pre_save` plugin and as such it used the `_all_fields` mask with all object data present to calculate the fields. The callback receives the current context of the data cell and writes back the result into the JSON response of the plugin.\n\n## Javascript\n\nThe Javscript snippet is defined as the body of the function. This code runs within an asynchronous function, meaning you can use `async` and `await` within it.\n\n```\nasync function (objNew, objCurr, dataPath, dataPathCurr) {\n    ... snippet as defined in the web frontend ...\n}\n```\n\nIt is executed using `eval`, a `try..catch` catches Exception. If an Exception is caught the event is written using the type `FORMULA_COLUMNS_ERROR`.\n\n* `objNew`: The new data of the currently saved object. This includes the record for top level objects, the nested record for nested objects.\n* `objCurr`: This data is the data as found in the database. It represents the old / current version of the data.\n* `dataPath`: During the recursive crawling of the data, the dataPath is extended for each iteration. It contains the path to the data starting from the top level.\n* `dataPathCurr`: Same as `dataPath` but for the current object data.\n\n\u003e A known limitation is that the callback for a nested record does not know which record idx it is currently in.\n\n## Utility functions and use of async functions\n\nWe provide an utility function that can be used inside the formula.\n\n### `async apiSearchBySIDs(sids, mode)`:\n\nThis asynchronous function allows you to find objects in the instance using their `system_global_id`. It accepts either a single `sid` or an array of `sids` and supports different data formats: `long`, `short`, `long_inheritance`, `full`, and `standard`. The function returns an array containing the found objects, or an empty array if no objects are found.\n\n#### Example 1 - Fetching a single linked object by `sid`:\nIn this example, we fetch a single linked object by its `sid` and return the `category` field of the linked object.\n```javascript\nconsole.info(\"Example of fetching linked object by sid\");\nif (!objNew.linked) {\n    return \"No linked object provided.\";\n}\n\nconst sid = objNew.linked._system_object_id;\nconst linkedObjectData = await apiSearchBySIDs(sid);\n\nconsole.info(\"Objects found: \", linkedObjectData);\n\nif (linkedObjectData \u0026\u0026 linkedObjectData.length \u003e 0) {\n    // Retrieve the linked object's fields using its _objecttype\n    const linkedObjectType = linkedObjectData[0]._objecttype;\n    const linkedFields = linkedObjectData[0][linkedObjectType];\n    return linkedFields.category;\n} else {\n    return \"Empty Linked Object...\";\n}\n```\nOr you can use your own code for fetching data, from fylr or external api.\n\n#### Example 2 - Using fetch inside the formula for fetching an external api endpoint:\nFormula code is inside an async function, so we can make use of async functions like fetch. In this example, we fetch an external API endpoint to get the final value of the field.\n```javascript\n// We define an async function to fetch the title of a todo from an external API\nasync function fetchTodoTitle() {\n    // Send a GET request to the sample API\n    console.info(\"Start fetching external data...\"); // This will be logged in the fylr console\n    // Use await to wait for the response, if not the function will return a promise, promises are not supported as return of the formula\n      const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');\n    if (!response.ok) {\n        throw new Error(`HTTP error! status: ${response.status}`);\n    }\n    // Parse the response as JSON\n    const data = await response.json();\n    // Return a string that includes the title of the todo this will be used as data for the target formula field\n    return `Todo Title: ${data.title}`;\n}\n\nreturn await fetchTodoTitle();\n\n```\n\n\n## Available Variables\n\nWithin the code, you have access to the following scoped variables:\n\n- `log`: Allows you to add messages that will be stored in system events.\n- `info`: Contains instance-related information, such as the `apiToken` or `url`. This is useful for making API calls.\n\nTo inspect the contents of `info`, you can use:\n\n```javascript\nlog.push({\"info\": info});","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogrammfabrik%2Ffylr-plugin-formula-columns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprogrammfabrik%2Ffylr-plugin-formula-columns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprogrammfabrik%2Ffylr-plugin-formula-columns/lists"}