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