Sharing JavaScript Functions In App

Hello!

Is there an easy way to share functions between queries and transformers? More specifically, my application uses MySQL with many columns that are datetime and whenever queries are done it needs to be formatted with moment. The end result is moment.utc(date).format("YYYY-MM-DD HH:mm:ss") is littered all over the queries. It would be much easier if I could move this into a function (ie formatDate(date)).

It looks like this could be done by triggering js queries, but needing to add the addtionalScope parameter is a little messy.

Any advice would be helpful!

Thank you

2 Likes

It's kinda hidden away, but you can create global javascript to use in all of your apps.

https://docs.retool.com/docs/custom-js-code

1 Like

That worked. Thank you!

That didn't worked for me. I put in the preloaded javascript:

window.computeAmount = (transactionType, scpiId, nbShares, duration) => {
  const scpi = {{scpis}}.data.find(v => v._id === scpiId)
  const sharePrice = scpi?.general?.buyingPriceByShare
  if (transactionType === "PP") return sharePrice * nbShares
  const partitionKey = scpi?.partitions.find(v => v.years === duration)?.partition
  return sharePrice * nbShares * partitionKey
}

but in an input (ie in default value of an input), if I want to use it, the function window.computeAmount or computeAmount is nowhere to be seen.

Spitballing here:

Have you tried a more traditional function syntax:

window.computeAmount = function(transactionType, scpiId, nbShares, duration) {

I can easily see a few reasons using an arrow function simply works differently in this scenario. Regular vs Arrow Function. Define your functions in many ways. | by Suprabha Supi | Geek Culture | Medium.

Also, I have noticed that it can take a page reload or two to get the function to be recognized.

Additionally, if there is a syntax error in the function it will not be created and thus not available. This last one has gotten me more times than I care to count. Let Chrome (or equivalent) Dev Tools check that for you - though sometimes it fails to notify of an error when there is one - but stepping through the code reveals this. Ain't programming fun!

Hi all,

For those who have stumbled across this thread -- we just launched a closed alpha for Sync Functions. Let me know if you'd like to be added!

Best,
Erin

1 Like

Hello all again, providing a quick update here. Earlier this year in March 2025, we launched App Functions Beta, which is the updated version of the Sync Functions Erin mentioned above.

App functions enable you to write reusable logic with dynamic parameters. Use functions when you want to perform the same actions on different pages, with different parameters, and return different data.

We’ve made updates in the app around how to write reusable logic, so check out the latest docs on App Functions! This feature is currently in beta, so let us know if you’d like to be added! :folded_hands:t3: